Django

Code

Ticket #11196 (closed: fixed)

Opened 10 months ago

Last modified 9 months ago

dimensions not set when value for ImageField specifed in constructor

Reported by: gwilson Assigned to: gwilson
Milestone: 1.1 Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

05/25/09 11:50:22 changed by gwilson

  • owner changed from nobody to gwilson.
  • needs_better_patch changed.
  • status changed from new to assigned.
  • needs_tests changed.
  • needs_docs changed.

05/28/09 00:21:40 changed by gwilson

  • stage changed from Unreviewed to Accepted.
  • milestone set to 1.1.

05/28/09 00:46:10 changed by gwilson

  • status changed from assigned to closed.
  • resolution set to fixed.

(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.

06/12/09 22:27:25 changed 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.

Add/Change #11196 (dimensions not set when value for ImageField specifed in constructor)




Change Properties
Action