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

Mahabubur Rahman
0


Method - 1:

You can use Q objects for this. They can be negated with the ~ operator and combined much like normal Python expressions:

from app_name.models import Entry

from django.db.models import Q


Entry.objects.filter(~Q(category =5))


will return all entries except category 5:


[<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, ...]


Method - 2:

You can use exclude 

from myapp.models import Entry

 

Entry.objects.exclude(category=5)


Will return all entries except category 5:

[<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, ...]

 

 

Tags

Post a Comment

0Comments
Post a Comment (0)