首页 > 解决方案 > django rest框架中的多个文件上传

问题描述

我想使用 DRF 创建一个 API,我想在其中上传两个不同名称的文件和一个带有 json 字符串的文本字段。下图显示了我对 API 的邮递员尝试。

在此处输入图像描述

请帮助我正确编写我的 API。我在堆栈溢出中查找了几篇帖子,但没有得到任何适当的解决方案。根据输入,我没有任何模型,但我想创建一个独立的 API,并想在运行时操作这些文件和 json。

标签: pythondjangodjango-rest-framework

解决方案


简单的类似,

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")

推荐阅读