Different list / retrieve serialization for ModelViewSet

2,579 views
Skip to first unread message

Kai Schlamp

unread,
May 11, 2013, 3:54:01 PM5/11/13
to django-res...@googlegroups.com
Hello together.

What is the best way to have different serialized fields for list and retrieve. When listing objects I want less fields included than when retrieving a single object (which is quite a common case I guess). Do I have to use different (custom) ViewSets for this or different serializers?

Kind regards,
Kai

Kai Schlamp

unread,
May 11, 2013, 4:19:33 PM5/11/13
to django-res...@googlegroups.com
What seems to work fine is to overwrite the list method of ModelViewSet and set another serializer_class (so having different serializers for one ModelViewSet). Is this the recommended way?

Tom Christie

unread,
May 12, 2013, 7:54:06 AM5/12/13
to django-res...@googlegroups.com
I'd recommend overriding get_serializer_class and returning a different serializer in each case.


You can inspect self.action inside a viewset to determine if its a list or retrieve action, or check self.kwargs for the pk arg.


--
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.
 
 
Reply all
Reply to author
Forward
0 new messages