Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Queryset on empty ImageField

Tags:

django

I have the following class:

class Book(models.Model):
    picture = models.ImageField(upload_to='books/', blank=True, null=True)
    ...

I now want to filter the books without a picture. I tried the following:

Book.objects.filter(picture__isnull=True)

The problem is, that the picture is an empty varchar ('') in the db and not null. What to do?

like image 995
melbic Avatar asked Mar 26 '13 09:03

melbic


People also ask

How do I do a not equal in Django Queryset filtering?

To do a not equal in Python Django queryset filtering, we can negate a equal with ~ . to call filter with the Q object negated with ~ to return all the Entry results that don't have id 3.

What is Queryset?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.


2 Answers

Try this:

Book.objects.filter(picture__exact='')
like image 142
arulmr Avatar answered Sep 28 '22 20:09

arulmr


Just run the exclude method:

Books.objects.exclude(picture='')
like image 42
0x4ndy Avatar answered Sep 28 '22 19:09

0x4ndy