[Fixed]-'getattr(): attribute name must be string' error in admin panel for a model with an ImageField



Your problem is with height_field=80 and width_field=80 these should not contain the height and width you require but rather the names of fields in your model that can have the values for height and width save in them.

As explained in the Django documentation for the ImagedField these are attributes on your model which will be populated for you when the model is saved. If you want this information populated for you the create model attribute where this information can be stored otherwise just remove these attributes they are optional.

๐Ÿ‘คTendayi Mawushe


The problem is probably this:

height_field=80, width_field=80

height_field and width_field, if you use them, are supposed to be names of fields on the model which contain the height and width information. Fix this it should work.

๐Ÿ‘คDaniel Roseman


Basically the

width_field and height_field

is used by django to refer to the model-field that is will be used to store the value of the width and height of the image.

try this instead;

class UserProfile(models.Model):
    "Additional attributes for users."
    url = models.URLField()
    location = models.CharField(max_length=100)
    user = models.ForeignKey(User, unique=True)
    avatar = models.ImageField(upload_to='/home/something/www/avatars', height_field="img_height", width_field="img_height")
    img_width = models.PositiveIntegerField()
    img_height = models.PositiveIntegerField()

    def __str__(self):
        return "Profile of " + self.user.username

Leave a comment