Drf-yasg: Set description of response of a list method

Created on 16 Jul 2018  Â·  3Comments  Â·  Source: axnsan12/drf-yasg

How can I set the description of a response that creates a list? My current code looks like

from rest_framework import mixins, viewsets

class WidgetViewSet(
        mixins.ListModelMixin,
        viewsets.GenericViewSet):
    serializer_class = WidgetSerializer

    @swagger_auto_schema(
        responses={
            200: openapi.Response(
               description='The list of widgets. Important regulatory information here.',
            ),
        }
    )
    def list(self, request):
        """ List all widgets the user can access """
        return super().list(request)

However, this makes the schema empty. I can omit 200, responses, or the whole @swagger_auto_schema, but then I don't get the response description.

If I add schema=WidgetSerializer to the parameters of 200: openapi.Response, the swagger doc lists only one widget instead of an array of widgets.

I can manually write out the swagger schema as a JSON-serializable nested dict, but that means I have to duplicate the information stored in WidgetSerializer.

How am I supposed to add descriptions to a list view?

question

Most helpful comment

You can:

  • pass a serializer instance - WidgetSerializer(many=True)
  • read the docs

All 3 comments

You can:

  • pass a serializer instance - WidgetSerializer(many=True)
  • read the docs

Thanks!

WidgetSerializer(many=True) works fine.

I did read the docs, but was unfamiliar with the many argument to Django serializers.

I was specifically referring to this line:

responses (dict[str,(Schema,SchemaRef,Response,str,Serializer)]) –
...

  • if a plain string is given as value, a Response with no body and that string as its description will be generated
Was this page helpful?
0 / 5 - 0 ratings

Related issues

therefromhere picture therefromhere  Â·  3Comments

beaugunderson picture beaugunderson  Â·  5Comments

geekashu picture geekashu  Â·  5Comments

hnykda picture hnykda  Â·  3Comments

maikotz picture maikotz  Â·  4Comments