本文介绍了使用Q对象时排除vs过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知道怎么可能:
A = object_list.filter( Q(sales__sale_starts__lte=today) & Q(sales__sale_ends__gte=today) )
# query inside filter catches 2 objects
B = object_list.exclude( Q(sales__sale_starts__lte=today) & Q(sales__sale_ends__gte=today) )
# query inside exclude catches 3 objects,
# though it is the same as previous
# in other words: object_list contains 20 objects,
# A - 2 objects, and B - 17 objects
使用 Q
对象时, filter()
和 exclude()
的工作方式是否有区别?谢谢.
Is there any difference in how filter()
and exclude()
working when using Q
objects? Thanks.
我希望 B
与以下内容相同:
I expect B
to be as in:
B = object_list.difference(A)
# B contains 18 objects
推荐答案
如@PauloAlmeida所说
As @PauloAlmeida said
由于不同的sql生成,因此 filter()
和 exclude()
之间确实存在差异.也许是 exclude()
中的错误.
So there is indeed can be a difference between filter()
and exclude()
, because of different sql generation. And maybe bugs in exclude()
.
这篇关于使用Q对象时排除vs过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!