简体   繁体   English

Django rest 框架 drf-yasg swagger ListField 序列化程序的多个文件上传错误

[英]Django rest framework drf-yasg swagger multiple file upload error for ListField serializer

I am trying to make upload file input from swagger (with drf-yasg ), but when I use MultiPartParser class it gives me the below error:我正在尝试从swagger (使用drf-yasg )输入上传文件,但是当我使用MultiPartParser class 时,出现以下错误:

drf_yasg.errors.SwaggerGenerationError: FileField is supported only in a formData Parameter or response Schema

My view:我的看法:

class AddExperience(generics.CreateAPIView):
    parser_classes = [MultiPartParser]

    permission_classes = [IsAuthenticated]
    serializer_class = DoctorExperienceSerializer

My serializer:我的序列化器:

class DoctorExperienceSerializer(serializers.Serializer):
    diploma = serializers.ListField(
        child=serializers.FileField(allow_empty_file=False)
    )
    education = serializers.CharField(max_length=1000)
    work_experience = serializers.CharField(max_length=1000)

I also tried FormParser but it still gives me the same error.我也试过FormParser但它仍然给我同样的错误。 Also: FileUploadParser parser but it works like JsonParser :另外: FileUploadParser解析器,但它的工作方式类似于JsonParser

The OpenAPISchema ( OAS ) 2 doesn't support the multiple file upload (see issue #254 ); OpenAPISchema ( OAS ) 2 不支持多文件上传(参见 issue #254 ); but OAS 3 supports it (you can use this YML spec on a live swagger editer ( see this result )).OAS 3支持它(您可以在实时 swagger 编辑器上使用此 YML 规范参见此结果))。

Comes to the real issue, there is a section in the drf-yasg's doc ,谈到真正的问题, drf-yasg 的文档中有一节

If you are looking to add Swagger/OpenAPI support to a new project you might want to take a look at drf-spectacular, which is an actively maintained new library that shares most of the goals of this project, while working with OpenAPI 3.0 schemas.如果您希望将 Swagger/OpenAPI 支持添加到新项目,您可能想看看 drf-spectacular,这是一个积极维护的新库,它共享该项目的大部分目标,同时使用 OpenAPI 3.0 模式。

OpenAPI 3.0 provides a lot more flexibility than 2.0 in the types of API that can be described. OpenAPI 3.0 在可描述的 API 类型上提供了比 2.0 更大的灵活性。 drf-yasg is unlikely to soon, if ever, get support for OpenAPI 3.0. drf-yasg 不太可能很快(如果有的话)获得对 OpenAPI 3.0 的支持。

That means the package drf-yasg doesn't have support for OAS3 and thus, it won't support the "multiple file upload" feature.这意味着 package drf-yasg不支持OAS3 ,因此,它不支持“多文件上传”功能。

You can consider migrating from drf-yasg to drf-spectacular .您可以考虑从drf-yasg迁移到drf-spectacular But, also note that, drf-spectacular is also dealing the FileUpload in a different way .但是,还要注意, drf-spectacular以不同的方式处理 FileUpload。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django - drf-yasg 将 @swagger_auto_schema 中的 request_body 设置为 @api_view 上的序列化程序的简化版本 - Django - drf-yasg setting request_body in @swagger_auto_schema to reduced version of serializer on @api_view Django 2.x drf-yasg如何在自定义方法中创建API(如在swagger中) - Django 2.x drf-yasg how to create API in a custom method (like in swagger) Django 如何在作为所有其他视图父级的类基视图中使用 drf-yasg 生成 swagger 文档? - Django How to use drf-yasg in class-base view which is parent of all other views to generate swagger docs? 自定义路径参数解析 drf-yasg 和 Django - Custom path parameter parsing drf-yasg and Django Django, drf-yasg - 如何为标签添加描述? - Django, drf-yasg - how to add description to tags? 使用 drf-yasg,我如何显示多个 openapi 方案? - With drf-yasg, how can i show multiple openapi schemes? 验证期间Django Rest Framework序列化程序丢失了ListField数据 - Django Rest Framework Serializer lost ListField data during validation 如何使用 drf-yasg 自动生成的招摇页面配置“HTTPS”方案? - How can I configure "HTTPS" schemes with the drf-yasg auto-generated swagger page? 如何在 drf-yasg swagger_auto_schema request_body 上指定示例值? - how to specify example value on drf-yasg swagger_auto_schema request_body? 在 drf-yasg 中设置自定义 basePath - set custom basePath in drf-yasg
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM