在注释的QuerySet上使用Q对象进行过滤 [英] Filtering with a Q object on an annotated QuerySet
本文介绍了在注释的QuerySet上使用Q对象进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个模拟测试用例:
A mock testcase:
def testCount(self):
qs = Test.objects.all()
qs = qs.annotate(a_count=Count('a_items'), b_count=Count('b_items'))
for item in qs:
print 'a_count: %d, b_count: %d' % (item.a_count, item.b_count)
qs1 = qs.filter(Q(a_count__gt=0))
self.assertEquals(qs1.count(), 1)
qs2 = qs.filter(Q(a_count__gt=0) | Q(b_count__gt=0))
self.assertEquals(qs2.count(), 1)
输出:
a_count: 1, b_count: 0
a_count: 0, b_count: 0
...
FAIL: testCount
self.assertEquals(qs2.count(), 1)
AssertionError: 0 != 1
为什么|操作员改变这样的行为,如何解决?
Why does the | operator change the behavior like this and how do I fix it?
推荐答案
看起来这仍然是一个开放问题从v1.2.4。
Looks like this is still an open issue as of v1.2.4.
我想你' d在错误报告中测试其中一个提供的修补程序,或者诉诸原始查询,直到它正式修复。
I guess you'd have test one of the supplied patches in the bug report or resort to raw queries until it is officially fixed.
这篇关于在注释的QuerySet上使用Q对象进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文