简体   繁体   中英

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. I am able to perform a POST request and the data is being sent to Django backend successfully. However, when I try to modify the data I get 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:

[ { "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:

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. But the error that I get is:

'Product' object has no attribute 'data'

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. 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.

  1. From the client-side(React) you need make FormData to send an image or file.
  2. In python every object has some special magic method one of them ar str , which method returns the string. When we print an object then this str method string was print except for print full object data. if you want to print full data you need to use print(product.__dict__) .

You can use the "patch" method to make your request instead of the "put" request. Patch uses partial_update() function therefore, you will need to define a partial_update() function as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM