Opened 15 years ago

Closed 15 years ago

Last modified 13 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: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 by Gary Wilson, 15 years ago

Owner: changed from nobody to Gary Wilson
Status: newassigned

comment:2 by Gary Wilson, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 by Gary Wilson, 15 years ago

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 by ccahoon, 15 years ago

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

milestone: 1.1

Milestone 1.1 deleted

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