Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#11196 closed (fixed)

dimensions not set when value for ImageField specifed in constructor

Reported by: gwilson Owned by: gwilson
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Given a model with an ImageField and defined height/width fields, ex.

class Person(models.Model):
    name = models.CharField(max_length=50)
    mugshot = ImageField(storage=temp_storage, upload_to='tests',
                         height_field='mug_height', width_field='mug_width')
    mug_height = models.PositiveSmallIntegerField()
    mug_width = models.PositiveSmallIntegerField()

Dimensions fields aren't set if you do:

p = Person(name='Joe', mugshot=ImageFile(open(...)))

Actually, they are getting set when the ImageField gets the value assigned in Model.__init__() (using the ImageFileDescriptor), but since the height and width fields are defined after the ImageField, they get set back to None by Model.__init__().

After calling save on the instance:

p.save()

...the dimensions fields are set because save() ends up calling ImageFileDescriptor.__set__(), which updates the dimensions fields.

This is related to #10044, #10404, and #11084.

Change History (5)

comment:1 Changed 6 years ago by gwilson

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to gwilson
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 6 years ago by gwilson

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by gwilson

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [10858]) Changes to ImageFileDescriptor and ImageField to fix a few cases of setting image dimension fields.

  • Moved dimension field update logic out of ImageFileDescriptor.__set__ and into its own method on ImageField.
  • New ImageField.update_dimension_fields method is attached to model instance's post_init signal so that:
    • Dimension fields are set when defined before the ImageField.
    • Dimension fields are set when the field is assigned in the model constructor (fixes #11196), but only if the dimension fields don't already have values, so we avoid updating the dimensions every time an object is loaded from the database (fixes #11084).
  • Clear dimension fields when the ImageField is set to None, which also causes dimension fields to be cleared when ImageFieldFile.delete() is used.
  • Added many more tests for ImageField that test edge cases we weren't testing before, and moved the ImageField tests out of file_storage and into their own module within model_fields.

comment:4 Changed 6 years ago by ccahoon

(In [10987]) Changes to ImageFileDescriptor and ImageField to fix a few cases of setting image dimension fields.

  • Moved dimension field update logic out of ImageFileDescriptor.__set__ and into its own method on ImageField.
  • New ImageField.update_dimension_fields method is attached to model instance's post_init signal so that:
    • Dimension fields are set when defined before the ImageField.
    • Dimension fields are set when the field is assigned in the model constructor (fixes #11196), but only if the dimension fields don't already have values, so we avoid updating the dimensions every time an object is loaded from the database (fixes #11084).
  • Clear dimension fields when the ImageField is set to None, which also causes dimension fields to be cleared when ImageFieldFile.delete() is used.
  • Added many more tests for ImageField that test edge cases we weren't testing before, and moved the ImageField tests out of file_storage and into their own module within model_fields.

comment:12 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top