Skip to content Skip to sidebar Skip to footer

Django Image Field Throws Typeerror

I'm writing a simple website using django to display some pictures. In my models, I've defined an image model, and a category model to allow me to categorize each image: class Imag

Solution 1:

A class that implements the method __getitem__ allows you to use array indexes on it. So MyClass[4] is (roughly) equivalent to MyClass.__getitem__[4].

Make sure you're not accidentally trying to use array indexers on your ImageField/ImageFieldFile.

Solution 2:

I ran into a similar issue with this class:

class Photo(models.Model):
    image_location = models.ImageField(upload_to='pix/%Y/%m/%d')
    caption = models.CharField(max_length=100)

    object = Manager()

You're seeing the error possibly because you're returning a model object (eg: Photo object):

#returning model object(eg: Photo object)def__unicode__(self):
    returnself.image_location

Instead of a unicode string (eg: /Path/to/my/pix):

#returning unicode string instead of model object(eg: /Path/to/my/pix)def__unicode__(self):
    return unicode(self.image_location)

This is the link to the StackOverflow answer that helped me: TypeError 'x' object has no attribute '__getitem__'

Post a Comment for "Django Image Field Throws Typeerror"