[Django]-Django admin save_model – assign new value to form field

1👍

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.

1👍

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()
👤max

Leave a comment