Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:14:24+00:00 2026-05-31T22:14:24+00:00

I have a model containing ImageField which should be resized after uploading. class SomeModel(models.Model):

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:14:26+00:00Added an answer on May 31, 2026 at 10:14 pm

    After several hours of trying to do this efficiently, I’ve changed my approach to this problem and defined CustomImageField like this:

    class CustomImageField(ImageField):
        attr_class = CustomImageFieldFile
    
        def __init__(self, resize=False, to_width=None, to_height=None, force=True, *args, **kwargs):
            self.resize = resize
            if resize:
                self.to_width = to_width
                self.to_height = to_height
                self.force = force
            super(CustomImageField, self).__init__(*args, **kwargs)
    
    
    class CustomImageFieldFile(ImageFieldFile): 
    
        def save(self, name, content, save=True):
            super(CustomImageFieldFile, self).save(name, content, save=save)
            if self.field.resize:
                resized_img = resize_image(filename=self.path,
                                           width=self.field.to_width,
                                           height=self.field.to_height,
                                           force=self.field.force)
                if resized_img:
                    setattr(self.instance, self.field.width_field, resized_img.size[0])
                    setattr(self.instance, self.field.height_field, resized_img.size[1])
    

    Now I can just define:

    class SomeModel(models.Model):
        my_image = CustomImageField(resize=True, to_width=SOME_WIDTH, to_height=SOME_HEIGHT, force=False,
                                    width_field='image_width', height_field='image_height')
        image_width = models.PositiveIntegerField(editable=False)
        image_height = models.PositiveIntegerField(editable=False)
    

    And depending on resize argument, image can be automatically resized after uploading, and width/height fields are correctly updated, without saving object twice. After quick tests it seems to be working fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a model containing: class MyModel(models.Model): id = models.IntegerField(primary_key=True) recorded_on = models.DateField()
I have a YML file containing fixtures for a Rails model (Comment) which looks
I have model Foo which has field bar. The bar field should be unique,
I have a model which looks like this: class test (db.Model) : tagList =
I have a LINQ model containing an entity which is populated by a stored
I have an old Xcode project which contains a CoreData model (containing a version
Lets say I have model inheritance set up in the way defined below. class
I think I don't unterstand django-haystack properly: I have a data model containing several
in a simple drawing application I have a model which has a NSMutableArray curvedPaths
I have a postgresql table containing movements of different items (models) between warehouses. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.