Skip to content

ImageField not populate width/height attributes #8133

@roman-rr

Description

@roman-rr

Create an object with ImageField within REST API are not automatically populated values into width_field and height_field.
Create the same object from django admin - width and heights are properly passed.

I dig deeper issue and found following things.
Dimensions assigned in ImageFileDescriptor setter.
Setter __set__ from ImageFileDescriptor function executed only once, right after model execute .save() method.
When create object from django admin, this setter executed few times before .save() method.

Currently i solve this issue with hack:

class Photo(models.Model):
  photo = models.ImageField(
        height_field='photo_height',
        width_field='photo_width',
  )

  class Meta:
        ordering = ('-created_at',)

  def save(self, *args, **kwargs):
        # add additional setter call before save model to re-assign width/height into our fields
        self.photo = self.photo 
        return super(CommentPhotoRel, self).save(*args, **kwargs)

But i believe that there are some reasons, setter called only one time after save, rather than admin model creation, where setter called few times before save.

Any thoughts ?

Checklist

  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions