django-imagekit
django-imagekit copied to clipboard
ImageField not populated width/height for child model
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
- 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
- Go to
http://localhost:8000/upload/
and upload any image to model. Width and height will be 0. - Make Photo class abstract = True and repeat uploading. Width and height will be populated as expect.
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.
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.