python - django rest框架中的多个文件上传
问题描述
我想使用 DRF 创建一个 API,我想在其中上传两个不同名称的文件和一个带有 json 字符串的文本字段。下图显示了我对 API 的邮递员尝试。
请帮助我正确编写我的 API。我在堆栈溢出中查找了几篇帖子,但没有得到任何适当的解决方案。根据输入,我没有任何模型,但我想创建一个独立的 API,并想在运行时操作这些文件和 json。
解决方案
简单的类似,
import json
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['POST'])
def foo(request):
attributes = json.loads(request.data['attributes']) # will be a 'dict'
xsd_file = request.data['xsd_file'] # "InMemoryUploadFile" instance
Iccs_file = request.data['Iccs_file'] # "InMemoryUploadFile" instance
return Response("some response")
推荐阅读
- vee-validate - 如何在不同名称的 vee-validate 中显示服务器错误?
- ruby-on-rails - 如何根据条件验证密码?
- javascript - 如何访问从 MVC 视图中的 ajax 调用调用的控制器返回的数据
- android - Jetpack Compose 中的高度垫片 (HeightSpacer)
- angular - 在 Ionic 4 中使用查询参数
- python - (Maya Python) 如何通过 GUI 函数运行单选选项按钮?
- python - 如何计算 python pandas 数据帧的第一条和最后一条记录之间的时间差异
- azure - Azure 管道“WinRMCustomScriptExtension”基础连接在非公共 VM 中关闭
- python - 在没有附加模块的 lambda 函数中使用反 cos
- vim - 如何在 Vim 中始终将文本保存在缓冲区中>