Why Django update() do not update image file in media folder, but update everything else in update process.. Old image is in media folder, but there is no new image in folder.. When creating (using save()) everything is fine.. Any help, thanks in advance...
Here is the model class:
class Company(models.Model):
name = models.CharField(max_length=100)
companyAddress = models.CharField(max_length=70)
companyPhoneNumber = models.CharField(max_length=30)
companyDescription = models.CharField(max_length=150)
companyProfileImage = models.ImageField(upload_to='images/', default = "images/defaultCompany.jpg", null = True)
Here is the code that I use when update:
newName = request.data['name']
newAddress = request.data['address']
newPhoneNumber = request.data['phoneNumber']
newDescription = request.data['description']
newCompanyProfileImage = request.data['image']
Company.objects.filter(id = companyId).update(name = newName, companyAddress = newAddress,
companyPhoneNumber = newPhoneNumber, companyDescription = newDescription, companyProfileImage = newCompanyProfileImage)
The
.updatemethod bypasses the model'ssavemethod.As noted in the docs:
Because you are using
.update, you are bypassing thesavemethod and therefore the image is not saved to disk.You must either (1) use
.saveto update the image OR (2) access the field storage class to 'manually' place the image in the correct location according to the configured storage backend (tricky, potentially unreliable so should be done in a transaction).Since you're just updating one object, just using the
.savemethod is probably the most straightforward solution: