ImageField is not updating when update() method is used in Django Serializer

I want to give a Admin the ability to update the image associated to a Product record. I have an edit class that allows the admin to update various elements of a record all of which are updating correctly except for the image fields. The problem is uploading image is working in Creating but not in Updating.


image1 = models.ImageField(db_column='product_image1', null=True, blank=True, upload_to='media/images/')

Serializer :

class products_update(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields =['category','product_name','description','unit_price','dis_price','image1','image2','image3']

Views :


                            description=description, quantity=quantity, unit_price=unitprice,
                            dis_price=discountprice,user=usertable, category=categoryid, image1=image1)

Image is successfully uploaded to media folder and path is stored in Database.


Product.objects.filter(id=pid, user=userdata).update(category = category, product_name=productname, description=description,unit_price=unitprice, dis_price=discountprice, image1=image1

When i use Update method image path is storing in DB, but Image is not storing in Media folder.

can anyone have an idea how to solve this issue

<queryset>.update(...) only adds kind of annotations to the queryset and affects only SQL query generation and does not call method. However only operates with model instances and calls fields save method which reaches file storage. A queryset (<model>.objects.filter().update()) cannot not do anything with file storages.

So instead of writing an update query you should instantiate model instance and save it. DRF documentation has examples of implementing serializers which save instances (as model instances, not direct DB update)

You use ModelSerializer which by default does call in update method implementation thus it is unclear how you got to your implementation. Just follow the docs and let happen.

Back to Top