def save_model(self, request, obj, form, change):
basewidth = 650
img = PIL.Image.open(form.cleaned_data['image_file'])
if img.size[0] > basewidth:
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
form.cleaned_data['image_file'] = img
form.save()
else:
form.save()
this is still saving the original image, not the resized one.
form.cleaned_data['image_file'] = img
this line looks wrong. how can I assign the new resized image to form field?
If you take a look at the docs, https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model you can see that obj
is the model instance. You would need to change obj.your_image_field
instead of the form field.
What @Ngenator said is correct. The reason that it is not working for you is that you need to also change form.save() to obj.save()
Here is the code I'm sure will do it:
def save_model(self, request, obj, form, change):
basewidth = 650
img = PIL.Image.open(obj.image_file)
if img.size[0] > basewidth:
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
obj.image_file = img
obj.save()
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.