Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An idea to support per-method serializer in a Viewset #112

Open
limdauto opened this issue Jul 11, 2016 · 1 comment
Open

An idea to support per-method serializer in a Viewset #112

limdauto opened this issue Jul 11, 2016 · 1 comment

Comments

@limdauto
Copy link
Contributor

I've implemented something like this on my fork

from my_serializers import PasswordSerializer
from rest_framework_docs.utils import func_serializer

class UserViewSet(viewsets.ViewSet):

    @func_serializer(serializer_class=PasswordSerializer)
    @detail_route(methods=['post'])
    def set_password(self, request, pk=None):
        serializer = PasswordSerializer(data=request.data)

I wonder if you are keen on extending your library with something like this? I think it's a handy feature.

The downside of this is that we have to change application code to support documentation generation. Another approach is to use docstring, something like


class UserViewSet(viewsets.ViewSet):

    @detail_route(methods=['post'])
    def set_password(self, request, pk=None):
        """
        :func_serializer: my_serializers.PasswordSerializer
        """
        serializer = PasswordSerializer(data=request.data)

I think I like the first approach better.

@limdauto
Copy link
Contributor Author

Actually the docstring approach doesn't look half-bad

    @detail_route(methods=['POST', 'DELETE'])
    def bar(self, request, version, pk):
        """
        :serializer: my_serializer.FooSerializer
        """

We can allow setting the regex pattern to grep for this in the docstring. To get the module from the above docstring, the regex pattern is r'(?<=\:serializer\:\s)(.+)(?=\b)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant