Filtering with Q Object in Annotated QuerySet

Testcase layout:

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) 

Output:

 a_count: 1, b_count: 0 a_count: 0, b_count: 0 ... FAIL: testCount self.assertEquals(qs2.count(), 1) AssertionError: 0 != 1 

Why? operator changes behavior like this, and how to fix it?

+4
source share
1 answer

This appears to be still an open issue as of version 2.1.4.

I assume that you would check one of the provided patches in the error report or resort to unprocessed requests prior to official fixing.

+2
source

Source: https://habr.com/ru/post/1333445/


All Articles