[Django]-Django rest framework – How to add post parameters to api document(drf_yasg)?

34πŸ‘

βœ…

The problem is that you are adding parameters of type form to swagger, but your view seems to expect a json payload in the request body. In that case you probably want to use request_body with an openapi.Schema object.

@swagger_auto_schema(method='post', request_body=openapi.Schema(
    type=openapi.TYPE_OBJECT, 
    properties={
        'x': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
        'y': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
    }
))
@api_view(['POST'])
def test(request):
    pass

This will automatically wrap your Schema into a Parameter of in_=openapi.IN_BODY.
See https://drf-yasg.readthedocs.io/en/stable/openapi.html for details.

Of course, the preferred way would be to use a class-based GenericAPIView together with a Serializer, which would simplify both the view code and the doc introspection.

πŸ‘€axnsan

Leave a comment