[英]Django Rest Framework 3: Uploading files without request.FILES
I noticed that I with Django Rest Framework 3 no longer have to specify files=request.FILES
when using a serializer in file uploads. 我注意到使用Django Rest Framework 3的人在文件上传中使用序列化程序时不再需要指定
files=request.FILES
。 According to the release notes , request.data
now contains all the data. 根据发行说明 ,
request.data
现在包含所有数据。 So, I simply removed files=request.FILES
, and went from: 因此,我只是删除了
files=request.FILES
,并且从:
serializer = MediaSerializer(
theMedia,
data = postData,
files = request.FILES,
partial = True
)
to: 至:
serializer = MediaSerializer(
theMedia,
data = postData,
partial = True
)
In my Django API app, I have a /media/ endpoint to which one can PUT
the files photo
and filename
. 在我的Django API应用程序中,我有一个/ media /端点,可以将文件
photo
和filename
PUT
到该端点。 When trying to PUT
files with the change I made, the files never touch my custom storage. 当尝试使用所做的更改
PUT
文件时,这些文件永远不会触碰我的自定义存储。 The header is correct (Content-Type: multipart/form-data), and it all worked fine in DRF 2.x. 标头是正确的(Content-Type:multipart / form-data),并且在DRF 2.x中都可以正常工作。
I Noticed that I need to use the MultiPartParser
in my view to be able to use the fileField
, so now my view uses parser_classes = (JSONParser, MultiPartParser)
. 我注意到我需要在视图中使用
MultiPartParser
才能使用fileField
,所以现在我的视图使用parser_classes = (JSONParser, MultiPartParser)
。
Here is the section of the serializer handeling the files: 这是序列化程序处理文件的部分:
class MediaSerializer(serializers.ModelSerializer):
photo = serializers.ImageField(
max_length = None,
required = False,
allow_empty_file = False)
filename = serializers.FileField(
max_length = None,
required = False,
allow_empty_file = False)
...
class Meta:
model = Media
fields = (
'photo',
'filename'
)
Maybe I should also mention that I have turned off UPLOADED_FILES_USE_URL
in the settings. 也许我还应该提到我已经关闭了设置中的
UPLOADED_FILES_USE_URL
。
My question is: What is it that I am missing out in DRF3 - Why does the uploaded files never touch my custom storage? 我的问题是:DRF3中缺少什么-为什么上传的文件从不触碰我的自定义存储?
Django REST Framework 3 deprecated the request.DATA
and request.FILES
properties in favor of a unified request.data
property . Django REST Framework 3弃用了
request.DATA
和request.FILES
属性,转而使用统一的request.data
属性 。 The serializers were also updated as well to remove the files
argument, still maintaining the data
argument which should be request.data
instead of request.DATA
. 序列化程序也进行了更新,以删除
files
参数,但仍保持应为request.data
而不是request.DATA
的data
参数。
In your example, you appear to be altering the incoming data, as you have it set to a variable postData
, which isn't included. 在您的示例中,您似乎正在更改传入的数据,因为您已将其设置为不包含的变量
postData
。 Most likely, you are not using request.data
and the issue is that you are only getting the data without the files, instead of the full data you are expecting. 最有可能的是,您没有使用
request.data
,问题是您只获取没有文件的数据,而不是所期望的完整数据。
You should be able to verify this by logging the values of request.data
and postData
, and check that request.data
contains the image you are trying to send. 您应该能够通过记录
request.data
和postData
的值来验证这postData
,并检查request.data
包含要发送的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.