#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.
Change History (5)
comment:1 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 15 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:4 by , 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 onImageField
. - New
ImageField.update_dimension_fields
method is attached to model instance'spost_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 withinmodel_fields
.
(In [10858]) Changes to
ImageFileDescriptor
andImageField
to fix a few cases of setting image dimension fields.ImageFileDescriptor.__set__
and into its own method onImageField
.ImageField.update_dimension_fields
method is attached to model instance'spost_init
signal so that:ImageFieldFile.delete()
is used.file_storage
and into their own module withinmodel_fields
.