Django - How to add swagger auto schema to DRF tagged functions with @api_view?
I have this function
view.py
@api_view(['POST'])
@swagger_auto_schema(
request_body=PostSerializer,
operation_description="Create a post object"
)
def post_create_post(request):
But the request body data requirements aren't showing up in Swagger UI. How do you add swagger documentation to endpoints created using @api_view? Also I'd ideally like to add a list of parameters with their types to the swagger schema.
Issue is ordering
@swagger_auto_schema(
method='post',
request_body=PostSerializer,
operation_description="Create a post object"
)
@api_view(['POST'])
def post_create_post(request):
you also need to add an method param to @swagger_auto_schema
Decorators are applied first for those 'closest' to the function definition. In order to use the @swagger_auto_schema decorator, the @api_view decorator must first be applied.
Hence, the corrected version is:
@swagger_auto_schema(
request_body=PostSerializer,
operation_description="Create a post object"
)
@api_view(['POST'])
def post_create_post(request):
Editorial: the other answer states:
you also need to add an method param to @swagger_auto_schema
However, for routes which have only one method, it's not necessary to specify it, as it will be assumed for you, at least in the current version of the drf_yasg library.