I have a model containing ImageField
which should be resized after uploading.
class SomeModel(models.Model):
banner = ImageField(upload_to='uploaded_images',
width_field='banner_width',
height_field='banner_height')
banner_width = models.PositiveIntegerField(_('banner width'), editable=False)
banner_height = models.PositiveIntegerField(_('banner height'), editable=False)
def save(self, *args, **kwargs):
super(SomeModel, self).save(*args, **kwargs)
resize_image(filename=self.banner.path,
width=MAX_BANNER_WIDTH,
height=MAX_BANNER_HEIGHT)
resize_image
is a custom function which does the resizing, and everything works fine, except that banner_width and banner_height are populated with dimensions of original image, before resizing.
Actual size of resized image may be smaller than MAX values given, so I have to open resized file to check it's actual dimensions after resizing. I could then manually set banner_width
and banner_height
, and save again, but it's not efficient way.
I could also do the resizing first, set width and height fields, and then save, but file at location self.banner.path
doesn't exist before save is performed.
Any suggestions on how should this be done properly?