django-rest-framework-docs
django-rest-framework-docs copied to clipboard
An idea to support per-method serializer in a Viewset
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.
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)'