Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ImageField issue

I have a similar model

Class Student(models.Model):
"""A simple class which holds the basic info
of a student."""

name = models.CharField(max_length=50)
age = models.PositiveIntegerField()
photo = models.ImageField(upload_to='foobar', blank=True, null=True)

As we can see photo field is optional. I wanted all the students who have their images saved in the college DB. For that i did this

>>> Student.objects.exclude(photo__name=None)

But i am getting this error :

FieldError: Join on field 'photo' not permitted.

So, How can i extract all those students having their photos?

Any sort of help regarding this would be appreciated. Thanks in advance.

like image 822
aatifh Avatar asked May 12 '09 08:05

aatifh


1 Answers

It doesn't work because field lookups only work on other models. Here, name is an attribute on the return value of your photo field.

Try this instead:

Student.objects.exclude(photo__isnull=True)

It is preferred to use isnull instead of comparing equality to None.

EDIT:

Jeff Ober's suggestion:

Student.objects.exclude(photo='')

He says filtering is performed on the actual values stored in DB. In a file field's case the path to the file.

like image 155
muhuk Avatar answered Oct 01 '22 07:10

muhuk