ModelViewSet - why aren't foreign key fields returned

226 views
Skip to first unread message

Tim Nelson

unread,
Dec 11, 2013, 10:20:39 AM12/11/13
to django-res...@googlegroups.com
I have seen that model.object.all() will get all fields including those in foreign key tables.

I have a simple Inventory model that references by foreign key and Item model.  My ViewSet inherits from ModelViewSet, but when I do a list on the view set only the foreign key PK is returned not the actual foreign key fields.

Is there a way to get the foreign key fields returned?

Rodrigo Gadea

unread,
Dec 11, 2013, 2:07:21 PM12/11/13
to django-res...@googlegroups.com
Mmm... I'm not sure if I am understanding you, did you mean to have a field like "relmodel_id"?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Tim Nelson

unread,
Dec 11, 2013, 3:35:39 PM12/11/13
to django-res...@googlegroups.com
For example I have Inventory that has a store and price and Item that has a description and sku.  The foreign key is in Inventory pointing to Item.

The ModelViewSet against Inventory is only returning the Inventory Model data (and the item.id FK) which I assumed would also return the related data (description and sku) from item.

If I just do a simple Inventory.objects.all() I do get the related data, I thought the MOdelViewSet would do that as well by default.


On Wednesday, December 11, 2013 2:07:21 PM UTC-5, Rodrigo Gadea wrote:
Mmm... I'm not sure if I am understanding you, did you mean to have a field like "relmodel_id"?
On Wed, Dec 11, 2013 at 3:20 PM, Tim Nelson <tim...@gmail.com> wrote:
I have seen that model.object.all() will get all fields including those in foreign key tables.

I have a simple Inventory model that references by foreign key and Item model.  My ViewSet inherits from ModelViewSet, but when I do a list on the view set only the foreign key PK is returned not the actual foreign key fields.

Is there a way to get the foreign key fields returned?

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Rodrigo Gadea

unread,
Dec 11, 2013, 3:45:20 PM12/11/13
to django-res...@googlegroups.com
OK, it's because ind you have to specify how that relation is represented, re-read from here:


To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages