[英]Django DRF Unable to send PUT request with Image Field
I have a form in React through which I am sending some data which includes an ImageField.我在 React 中有一个表单,我通过它发送一些包含 ImageField 的数据。 I am able to perform a POST request and the data is being sent to Django backend successfully.
我能够执行 POST 请求,并且数据已成功发送到 Django 后端。 However, when I try to modify the data I get employee_image: `
但是,当我尝试修改数据时,我得到了 employee_image: `
["The submitted data was not a file. Check the encoding type on the form."]
[“提交的数据不是文件。请检查表单上的编码类型。”]
This is the data coming up on GET request:这是 GET 请求中出现的数据:
[ { "id": 2, "product_name": "XYZ", "product_description": "XYZ", "product_price": "12.00", "product_unit_weight": "120.00", "product_unit_weight_units": "GM", "product_type": "CPG", "product_image": "http://192.168.29.135:8000/media/product_images/xyz.png" },
[ { "id": 2, "product_name": "XYZ", "product_description": "XYZ", "product_price": "12.00", "product_unit_weight": "120.00", "product_unit_weight_units": "GM", "product_type ": "CPG", "product_image": "http://192.168.29.135:8000/media/product_images/xyz.png" },
This is my models.py file:这是我的 models.py 文件:
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
def post(self, request, *args, **kwargs):
product_name = request.data['product_name']
product_description = request.data['product_description']
product_price = request.data['product_price']
product_unit_weight = request.data['product_unit_weight']
product_unit_weight_units = request.data['product_unit_weight_units']
product_type = request.data['product_type']
product_image = request.data['product_image']
Product.objects.create(product_name=product_name, product_description=product_description, product_price=product_price, product_unit_weight=product_unit_weight, product_unit_weight_units=product_unit_weight_units, product_type=product_type, product_image=product_image)
return Response({'message':'Product created successfully'}, status=200)
def update(self, request, *args, **kwargs):
print('Put method running')
pk = self.kwargs.get('pk')
product = get_object_or_404(Product.objects.all(), pk=pk)
print(product)
if (product.product_image.startswith('http')):
img_name = dataDict["product_image"].split("/")[-1]
product_img_temp = ContentFile(request.get(dataDict["product_image"]).content, name=img_name)
dataDict['product_img'] = product_img_temp
serializer = ProductSerializer(product, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I tried following this response and edited my models.py with the update() as shown above.我尝试按照此响应并使用 update() 编辑我的 models.py,如上所示。 But the error that I get is:
但我得到的错误是:
'Product' object has no attribute 'data'
'产品' object 没有属性 '数据'
Also, When I am printing the product in the update() method, I am getting only the product name printed and not the rest of the fields.此外,当我在 update() 方法中打印产品时,我只打印了产品名称,而不是字段的 rest。 I am unable to understand why I am getting this issue.
我不明白为什么我会遇到这个问题。 Any help would be much appreciated.
任何帮助将非常感激。 Thanks in advance.
提前致谢。
` `
Here are several problems you have to overcome.这是您必须克服的几个问题。
print(product.__dict__)
.print(product.__dict__)
。You can use the "patch" method to make your request instead of the "put" request.您可以使用“patch”方法来发出您的请求,而不是“put”请求。 Patch uses partial_update() function therefore, you will need to define a partial_update() function as well.
补丁使用 partial_update() function 因此,您还需要定义一个 partial_update() function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.