Skip to content

get_serializer_class doesn't recreate same conditions as would be used by DRF #168

@optiz0r

Description

@optiz0r

Where get_serializer_class() is used to determine which serializer would be used for a ModelViewSet, it doesn't re-create the same environment as DRF would use when servicing an actual API request, and so it may return different results (and thus incorrect documentation).

As an example, I have the following method, which uses a cut-down serializer for list actions, and a full-content serializer for retrieve actions:

class FooViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Foo.objects.all()

    def get_serializer_class(self):
        if hasattr(self, 'action'):
            if self.action == 'list':
                return FooListSerializer
            elif self.action == 'retrieve':
                return FooSerializer

        return serializers.Default

drfdocs doesn't currently set the self.action member before calling get_serializer_class(), so it gets the default serializer with no fields, whereas an actual API request would get either the FooSerializer or the FooListSerializer.

Activity

mohammedzee1000

mohammedzee1000 commented on Nov 30, 2017

@mohammedzee1000

+1 to this I am not able to use versioned docs properly api thanks to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @optiz0r@mohammedzee1000

        Issue actions

          get_serializer_class doesn't recreate same conditions as would be used by DRF · Issue #168 · manosim/django-rest-framework-docs