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?
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.
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.
Try this:
Book.objects.filter(picture__exact='')
Just run the exclude method:
Books.objects.exclude(picture='')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With