D
D
Denis Startsev2016-02-21 14:39:24
Django
Denis Startsev, 2016-02-21 14:39:24

Why is an error thrown when setting the width and height attributes on an ImageField model field?

In the model, I specify the following field:

image = models.ImageField(upload_to='photos', height_field=60, width_field=60, max_length=100)

when I try to create an object in the admin panel, I get the following error:
TypeError at /admin/gallery/photo/
getattr(): attribute name must be string

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
marazmiki, 2016-02-21
@denisstarcev

Because these attributes do not set the width and height of the picture. They set the names of the fields in which djanga will write the actual width and height of the loaded image. Something like that:

class MyModel(models.Model):
    img_width = models.PositiveIntegerField(null=True)
    img_height = models.PositiveIntegerField(null=True)
    image = models.ImageField(upload_to='photos', height_field='img_height', width_field='img_width', max_length=100)

M
Maxim Zubenko, 2018-09-01
@JawsIk

Why is it even needed then?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question