【问题标题】:Filtering with a Q object on an annotated QuerySet在带注释的 QuerySet 上使用 Q 对象进行过滤
【发布时间】:2011-05-31 00:12:16
【问题描述】:

模拟测试用例:

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

为什么 |操作员改变这样的行为,我该如何解决?

【问题讨论】:

    标签: django django-queryset django-q


    【解决方案1】:

    从 v1.2.4 开始,这似乎仍然是 open issue

    我猜你应该测试bug report 中提供的补丁之一或求助于raw queries,直到它被正式修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 2016-09-07
      • 2011-10-24
      • 1970-01-01
      • 2010-12-29
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多