Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django QuerySet, how do I do negation in the filter?

Tags:

django

Filtering QuerySets in Django work like the following:

Entry.objects.filter(year=2006)

How can I use filter to find all entries which does not have year 2006? Something similar to the following sql:

SELECT * 
FROM entries
WHERE not year = 2006
like image 412
Thierry Lam Avatar asked Sep 02 '09 19:09

Thierry Lam


1 Answers

I think you are looking for the exclude() method:

>>> Entry.objects.exclude(year=2006)

Will return all Entry objects that are not in the year 2006.

If you wish to further filter the results, you can chain this to a filter() method:

>>> Entry.objects.exclude(year=2006).filter(field='value')
like image 162
Jesse L Avatar answered Oct 19 '22 05:10

Jesse L