如何使用 drf-yasg 在 django-rest-framework 中为文件上传 API 制作招摇模式? [英] How to make swagger schema for file upload API in django-rest-framework using drf-yasg?
问题描述
我找不到任何支持为文件上传 API 创建架构.Swagger UI 必须有一个按钮,允许测试人员上传文件以进行测试.我使用 firebase 作为数据库,因此序列化程序和模型不会出现.我只使用 Django 的 rest 框架.
I am not able to find any support for making a schema for the file upload API. The Swagger UI must have a button allowing a tester to upload a file for testing purposes. I am using firebase as a database so serializers and models don't come into the picture. I am using only Django's rest framework.
我查看了建议使用 Operation 进行文件上传的 drf-yasg 文档.但这是一个非常抽象和晦涩的文档.
I have looked at drf-yasg's documentation that suggests using Operation for file upload. But It is a very abstract and obscure documentation.
推荐答案
确保在视图中指定 parser_classes
.默认情况下,它是不处理文件上传的 JSON 解析器.使用 MultiPartParser
或 FileUploadParser
Make sure you specify the parser_classes
in your view. By Default it's JSON parser which doesn't handle file uploads. Use either MultiPartParser
or FileUploadParser
class MyUploadView(CreateAPIView):
parser_classes = (MultiPartParser,)
...
@swagger_auto_schema(operation_description='Upload file...',)
@action(detail=False, methods=['post'])
def post(self, request, **kwargs):
# Code to handle file
这篇关于如何使用 drf-yasg 在 django-rest-framework 中为文件上传 API 制作招摇模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!