How do I set a different Serializer for list and detail view while using viewsets.ModelViewSet
and HyperlinkedSerializer
?
I see how to do it with viewsets.ViewSet
by defining list
and retrive
, (here's an example) but I don't know how to elegantly adapt it to viewsets.ModelViewSet
I've adapted an answer from "Django rest framework, use different serializers in the same ModelViewSet" that serves me very well, and I hope you'll find useful:
In this case, you're just specifying your two serializers and using the one depending on the action. However, this can be made more general (for all actions) as follows:
Viewsets extend the class GenericAPIView, so you can use this part of the documentation to solve your problem. Basically, what you need is to override get_serializer_class and to return a different serializer based on your request.
I've created this small package for this job. drf_custom_viewsets.
It has
CustomSerializerViewSet
, which inherits fromModelViewSet
, which lets you set different serializers for different actions.