在Django查询过滤中如何做不平等? [英] How do I do a not equal in Django queryset filtering?
本文介绍了在Django查询过滤中如何做不平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Django模型QuerySets中,我看到有比较价值的 __ gt
和 __ lt
,但是在那里一个 __ ne
/ !=
/ <>
不等于?)
In Django model QuerySets, I see that there is a __gt
and __lt
for comparitive values, but is there a __ne
/!=
/<>
(not equals?)
我想使用不等于过滤:
示例:
Model:
bool a;
int x;
我想要
results = Model.objects.exclude(a=true, x!=5)
!=
语法不正确。我试过 __ ne
,<>
。
The !=
is not correct syntax. I tried __ne
, <>
.
我最终使用:
results = Model.objects.exclude(a=true, x__lt=5).exclude(a=true, x__gt=5)
推荐答案
也许 Q对象可能对此有帮助问题。我从来没有使用过它们,但是似乎它们可以被否定,并且与普通的python表达式很相似。
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>, ...]
这篇关于在Django查询过滤中如何做不平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文