25
votes

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

4

4 Answers

50
votes

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:

class MyModelViewSet(viewsets.MyModelViewSet):

    queryset = MyModel.objects.all()
    serializer_class = MyModelListSerializer
    detail_serializer_class = MyModelDetailSerializer

    def get_serializer_class(self):
        if self.action == 'retrieve':
            if hasattr(self, 'detail_serializer_class'):
                return self.detail_serializer_class

        return super(MyModelViewSet, self).get_serializer_class()

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:

class MyModelViewSet(viewsets.MyModelViewSet):

    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    action_serializers = {
        'retrieve': MyModelDetailSerializer,
        'list': MyModelListSerializer,
        'create': MyModelCreateSerializer
    }

    def get_serializer_class(self):

        if hasattr(self, 'action_serializers'):
            return self.action_serializers.get(self.action, self.serializer_class)

        return super(MyModelViewSet, self).get_serializer_class()
1
votes

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.

0
votes

I've created this small package for this job. drf_custom_viewsets.

It has CustomSerializerViewSet, which inherits from ModelViewSet, which lets you set different serializers for different actions.

0
votes

As of 2021, I will do it differently, a better and more generic way around is doing something like this:

class PlayersListViewSet(viewsets.ModelViewSet):
    queryset = Player.objects.all()
    serializer_class = PlayersListSerializer
    http_method_names = ['get', 'post']
    pagination_class = None
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['name']

    serializer_class_by_action = {
        'retrieve': PlayersDetailSerializer,
        'list': PlayersListSerializer,
    }

    def get_serializer_class(self):

        if hasattr(self, 'serializer_class_by_action'):
            return self.serializer_class_by_action.get(self.action, self.serializer_class)

        return super(MyModelViewSet, self).get_serializer_class()

    def get_queryset(self):
        queryset = Player.objects.all()
        team_id = self.request.query_params.get('team', None)

        if team_id:
            try:
                queryset = queryset.filter(team=team_id)
            except ValueError:
                raise exceptions.ParseError()
        return queryset

Here action is the method used by the serializer, list in case of def list, retrieve in case of def retrieve and so on..