How do I do a not equal in Django queryset filtering?
In Django model QuerySets, I see that there is a
__lt for comparitive values, but is there a
<> (not equals?)
I want to filter out using a not equals:
Model: bool a; int x;
results = Model.objects.exclude(a=true, x!=5)
!= is not correct syntax. I tried
I ended up using:
results = Model.objects.exclude(a=true, x__lt=5).exclude(a=true, x__gt=5)
Maybe Q objects could be of help for this problem. I've never used them but it seems they can be negated and combined much like normal python expressions.
Update: I Just tried it out, it seems to work pretty well:
>>> from myapp.models import Entry >>> from django.db.models import Q >>> Entry.objects.filter(~Q(id = 3)) [<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, ...]