Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#11196 closed (fixed)

dimensions not set when value for ImageField specifed in constructor

Reported by: Gary Wilson Owned by: Gary Wilson
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 7 years ago by Gary Wilson

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to Gary Wilson
Patch needs improvement: unset
Status: newassigned

comment:2 Changed 7 years ago by Gary Wilson

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 7 years ago by Gary Wilson

Resolution: fixed
Status: assignedclosed

(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 7 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 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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