首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >drf_yasg:如何在请求体中定义多部分/表单数据

drf_yasg:如何在请求体中定义多部分/表单数据
EN

Stack Overflow用户
提问于 2022-01-25 11:17:54
回答 1查看 1.4K关注 0票数 0

无法生成包含多部分/表单数据内容类型的swagger文件

描述

我有一个上传文件的请求,在那里我发送多部分/表单数据的文件。我试图将表单数据描述为

这就是我在邮递员中的要求。

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

最小再生产

代码语言:javascript
运行
复制
@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')
                })
            )
        }
    )
EN

回答 1

Stack Overflow用户

发布于 2022-03-29 06:53:41

在视图类中,需要设置MultiPartParser类,定义所使用的媒体类型:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70847708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档