vintasoftware / drf-rw-serializers

Generic views, viewsets and mixins that extend the Django REST Framework ones adding separated serializers for read and write operations
MIT License
177 stars 28 forks source link

swagger/openapi support #17

Closed kolotev closed 3 months ago

kolotev commented 5 years ago

Description

When I add swagger/openapi view support (with help of drf-yasg for example) I get undesirable/incorrect specification for writable methods (POST, PUT, PATCH), they correspond to read_serializer_class instead of to write_serializer_class.

What I Did

from drf_rw_serializers import viewsets

class MyViewSet(viewsets.ModelViewSet):
    read_serializer_class = serializers.ReadSerializer
    write_serializer_class = serializers.WriteSerializer

To fix that problem I suggest to add something along the following lines:

    def get_serializer_class(self):
        if self.request.method in ['GET']:
            return self.get_read_serializer_class()
        return self.get_write_serializer_class()

Hopefully I provided sufficient amount of information.

Thank you for your efforts.