Django - 使用“@api_view”将 Django Rest Swagger 架构添加到 DRF 标记函数

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

Django - 如何使用 @api_view?

将 swagger 自动模式添加到 DRF 标记函数

我有这个功能

view.py

@api_view(['POST'])
@swagger_auto_schema(
    request_body=PostSerializer,
    operation_description="Create a post object"
)
def post_create_post(request):

但是请求正文数据要求没有出现在 Swagger UI 中。如何将 swagger 文档添加到使用 @api_view 创建的端点?另外,理想情况下,我想将参数列表及其类型添加到 swagger 模式中。

订购有问题

@swagger_auto_schema(
    method='post',
    request_body=PostSerializer,
    operation_description="Create a post object"
)
@api_view(['POST'])
def post_create_post(request):

您还需要将 method 参数添加到 @swagger_auto_schema

装饰器首先应用于那些 'closest' 函数定义。要使用 @swagger_auto_schema 装饰器,必须先应用 @api_view 装饰器。

因此,更正后的版本是:

@swagger_auto_schema(
    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

然而,对于只有一种方法的路由,没有必要指定它,如it will be assumed for you,至少在当前版本的drf_yasg库中是这样。