[Django]-Django – Add Django Rest Swagger schema to DRF tagged functions with `@api_view`

2👍

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

0👍

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.

👤sytech

Leave a comment