9

I'm using Django REST Framework with django-rest-swagger to create browsable interface for my API. I can specify a request body serializer using YAML docstring, but I haven't found a way to specify a serializer for request query parameters. The view I am using is pretty like:

class ListBans(BaseBanView):

    def get(self, request):
        """
        List all profile bans
        ---
        response_serializer: backend_serializers.BanSerializer
        request_serializer: moderator_serializers.ListBansSerializer
        """
        serializer = moderator_serializers.ListBansSerializer(data=request.query_params)
        if serializer.is_valid(raise_exception=True):
            # query profile bans
            data = []
            return APIResponse(status=status.HTTP_200_OK, data=data)

class ListBansSerializer(serializers.Serializer):
    limit = serializers.IntegerField(default=10, help_text='query limit')
    offset = serializers.IntegerField(default=0, help_text='query offset')

what I am trying to achieve is to make django-rest-swagger create form fields for query parameters from ListBansSerializer so that I wouldn't have to specify the parameters section manually in the docstring. Is there a way to do that?

1 Answer 1

5

I've created a simple decorator that automatically appends query parameters to the docstring. The source is available at github. Here's a usage example:

class ListBansSerializer(serializers.Serializer):
    limit = serializers.IntegerField(default=10, help_text='query limit')
    offset = serializers.IntegerField(default=0, help_text='query offset')


class ListBans(BaseBanView):
    @add_query_parameters(ListBansSerializer)
    def get(self, request):
        """
        List all profile bans
        ---
        response_serializer: backend_serializers.BanSerializer
        """
        serializer = moderator_serializers.ListBansSerializer(data=request.query_params)
        if serializer.is_valid(raise_exception=True):
            # query profile bans
            data = []
            return APIResponse(status=status.HTTP_200_OK, data=data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.