Django rest framework, use different serializers in the same ModelViewSet

Override your get_serializer_class method. This method is used in your model mixins to retrieve the proper Serializer class.

Note that there is also a get_serializer method which returns an instance of the correct Serializer

class DualSerializerViewSet(viewsets.ModelViewSet):
    def get_serializer_class(self):
        if self.action == 'list':
            return serializers.ListaGruppi
        if self.action == 'retrieve':
            return serializers.DettaglioGruppi
        return serializers.Default # I dont' know what you want for create/destroy/update.                

Leave a Comment