【问题标题】:drf_yasg: How to define multipart/form-data in request bodydrf_yasg:如何在请求正文中定义多部分/表单数据
【发布时间】:2022-01-25 11:17:54
【问题描述】:

无法在请求中生成具有 multipart/form-data 内容类型的 swagger 文件

说明

我有一个上传文档的 POST 请求,我将在其中发送以 multipart/form-data 发送的文档。我试图这样描述表单数据

这就是我的请求在邮递员中的样子

当我尝试生成一个 swagger 文件时。它给了我以下错误 drf_yasg.errors.SwaggerGenerationError:当请求有请求体时无法添加表单参数;您是否忘记在视图上设置适当的解析器类?

最小复制

@swagger_auto_schema(
        operation_id='Create a document',
        operation_description='Create a document by providing file and s3_key',
        manual_parameters=[
            openapi.Parameter('file', openapi.IN_FORM, type=openapi.TYPE_FILE, description='Document to be uploaded'),
            openapi.Parameter('s3_key', openapi.IN_FORM, type=openapi.TYPE_STRING, description='S3 Key of the Document '
                                                                                               '(folders along with name)')
        ],
        responses={
            status.HTTP_200_OK: openapi.Response(
                'Success', schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={
                    'doc_id': openapi.Schema(type=openapi.TYPE_STRING, description='Document ID'),
                    'mime_type': openapi.Schema(type=openapi.TYPE_STRING, description='Mime Type of the Document'),
                    'version_id': openapi.Schema(type=openapi.TYPE_STRING, description='S3 version ID of the document')
                })
            )
        }
    )

【问题讨论】:

    标签: django-rest-framework swagger drf-yasg


    【解决方案1】:

    在您的 View 类中,您需要设置 MultiPartParser 类,定义您使用的媒体类型:

    from rest_framework.views import APIView
    from rest_framework.parsers import MultiPartParser
    
    class MyAPIView(APIView):
        parser_classes = [MultiPartParser]
    
        @swagger_auto_schema(
                operation_id='Create a document',
                operation_description='Create a document by providing file and s3_key',
                manual_parameters=[
                    openapi.Parameter('file', openapi.IN_FORM, type=openapi.TYPE_FILE, description='Document to be uploaded'),
                    openapi.Parameter('s3_key', openapi.IN_FORM, type=openapi.TYPE_STRING, description='S3 Key of the Document '
                                                                                                       '(folders along with name)')
                ],
                responses={
                    status.HTTP_200_OK: openapi.Response(
                        'Success', schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={
                            'doc_id': openapi.Schema(type=openapi.TYPE_STRING, description='Document ID'),
                            'mime_type': openapi.Schema(type=openapi.TYPE_STRING, description='Mime Type of the Document'),
                            'version_id': openapi.Schema(type=openapi.TYPE_STRING, description='S3 version ID of the document')
                        })
                    )
                }
            )
        def post(self, request, *args, **kwargs):
            # Content of the post method
    

    【讨论】:

    • 最后好像少了一些代码。
    • 是post方法的内容。这与问题无关,但我发表评论以表明这一点。
    猜你喜欢
    • 2018-06-10
    • 2021-09-18
    • 2018-10-01
    • 2010-11-03
    • 2019-05-17
    • 2022-12-05
    • 2012-11-27
    • 2016-12-17
    • 2012-03-16
    相关资源
    最近更新 更多