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?
You can:
WidgetSerializer(many=True)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
Most helpful comment
You can:
WidgetSerializer(many=True)