I have a model called ProductImage
that contains a few fields and an Django ImageField
. In this case I already have the object created, and I want to update the featured
boolean in the object.
我有一个名为ProductImage的模型,它包含几个字段和一个Django ImageField。在这种情况下,我已经创建了对象,并且我想要更新对象中具有特色的布尔值。
Problem is that when I do a $http.put()
(Using AngularJS) I get an error returned saying:
问题是,当我执行$http.put()(使用AngularJS)时,会得到一个返回的错误:
The submitted data was not a file. Check the encoding type on the form.
提交的数据不是一个文件。检查窗体上的编码类型。
My REST API Object looks like this on the GET request:
我的REST API对象在GET请求中是这样的:
{
"id": 15,
"image": "http://127.0.0.1:8000/media/products/photo_1_5.JPG",
"alt": "HelloWorld",
"featured": false,
"product": 1
}
The HTTP PUT request I send looks like this: (Notice featured
has been changed to true
)
我发送的HTTP PUT请求如下所示:
{
"id": 15,
"image": "http://127.0.0.1:8000/media/products/photo_1_5.JPG",
"alt": "HelloWorld",
"featured": true,
"product": 1
}
So... How do I update my object without having to re-submit/re-upload the image file?
所以…如何在无需重新提交/重新上传图像文件的情况下更新对象?
1 个解决方案
#1
1
If you use PUT
to update an object you have to send a full instance. So in your case you have to send a image file for image
not an url to the image.
如果使用PUT更新对象,则必须发送一个完整的实例。所以在你的情况下你必须为图像发送一个图像文件而不是一个url到图像。
The easiest solution is probably to use PATCH
instead of PUT
. Then you can do a partial update and send only the updated fields.
最简单的解决方案可能是使用PATCH而不是PUT。然后可以进行部分更新,只发送更新的字段。
{
"featured": true
}
#1
1
If you use PUT
to update an object you have to send a full instance. So in your case you have to send a image file for image
not an url to the image.
如果使用PUT更新对象,则必须发送一个完整的实例。所以在你的情况下你必须为图像发送一个图像文件而不是一个url到图像。
The easiest solution is probably to use PATCH
instead of PUT
. Then you can do a partial update and send only the updated fields.
最简单的解决方案可能是使用PATCH而不是PUT。然后可以进行部分更新,只发送更新的字段。
{
"featured": true
}