Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageField not populated width/height for child model #530

Open
roman-rr opened this issue Aug 19, 2021 · 2 comments
Open

ImageField not populated width/height for child model #530

roman-rr opened this issue Aug 19, 2021 · 2 comments

Comments

@roman-rr
Copy link

First of all, sorry if this repo is not related to width/height assignation to the model. I'm not well professional with django. If so, please redirect me to repository responsible for that.

I already create related issue in the django-rest-framework repository here
Because this issue i can reproduce only with rest-framework. With default admin POST form is not exist.

When i do POST request as form-date with imageField, the image successfully uploaded, but photo_width and photo_height are not being populated.

This is my models:

class Photo(models.Model):
    photo_height = models.PositiveIntegerField(
        default=0,
        verbose_name="Photo Height")
    photo_width = models.PositiveIntegerField(
        default=0,
        verbose_name="Photo Width")
    photo = models.ImageField(
        height_field='photo_height',
        width_field='photo_width',
    )
    # class Meta:
    #     abstract = True
        
class CommentPhotoRel(Photo):
    class Meta:
        verbose_name = 'Comment Photo Relation'
        verbose_name_plural = 'Comment Photo Relations'

CommentPhotoRel is extends Photo model, which is NON-abstract.
I use non-abstract parent to keep ability go through all photos.
If i switch this class to abstract issue will not reproduced and width/heigth will passed correctly.

Steps to reproduce

  1. Install
git clone https://github.com/roman-rr/imagekit-rest-bug.git
cd imagekit-rest-bug
python3 -m venv venv
source venv/bin/activate
pip install requirements.txt
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser
./manage.py runserver
  1. Go to http://localhost:8000/upload/ and upload any image to model. Width and height will be 0.
  2. Make Photo class abstract = True and repeat uploading. Width and height will be populated as expect.
@roman-rr roman-rr changed the title ImageField not populate width/height attributes for child class model ImageField not populated width/height for child model Aug 19, 2021
@vstoykov
Copy link
Collaborator

In the example code I can't see anything related to ImageKit. Just pure Django (at least for the models). Can you try to reroduce this but using simple Django model forms instead of using Django REST Framework? It's very strange that the save method is called so many times in the admin.

Also can you share with us the REST framework view responsible for the /upload/ endpoint and everything REST framework specific.

@roman-rr
Copy link
Author

Hello @vstoykov
I can reproduce only with Django REST Framework or with REST request from postman (to Django REST endpoint).
Only when parent class is not abstract.
In repo the minimal code to reproduce issue.

NOTE:
What i personally found:
Dimensions assigned in ImageFileDescriptor setter.
Setter __set__ from ImageFileDescriptor function executed only once when POST with Django REST, right after model execute .save() method.
When create object from django admin, this setter executed few times before .save() method.

Yes, maybe is not directly to this repo, more to Django + Django REST in pair. But i will appreciate any thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants