Fixed #30011 -- Fixed queries that reuse filtered aggregates.

Thanks Taqi Abbas and Raphael Kimmig for the report.
This commit is contained in:
Simon Charette 2018-12-06 10:40:49 -05:00 committed by Tim Graham
parent 88619e6129
commit 53269bcaaf
2 changed files with 9 additions and 1 deletions

View File

@ -27,7 +27,7 @@ class Aggregate(Func):
def get_source_expressions(self): def get_source_expressions(self):
source_expressions = super().get_source_expressions() source_expressions = super().get_source_expressions()
if self.filter: if self.filter:
source_expressions += [self.filter] return source_expressions + [self.filter]
return source_expressions return source_expressions
def set_source_expressions(self, exprs): def set_source_expressions(self, exprs):

View File

@ -79,3 +79,11 @@ class FilteredAggregateTests(TestCase):
msg = 'Star cannot be used with filter. Please specify a field.' msg = 'Star cannot be used with filter. Please specify a field.'
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
Count('*', filter=Q(age=40)) Count('*', filter=Q(age=40))
def test_filtered_reused_subquery(self):
qs = Author.objects.annotate(
older_friends_count=Count('friends', filter=Q(friends__age__gt=F('age'))),
).filter(
older_friends_count__gte=2,
)
self.assertEqual(qs.get(pk__in=qs.values('pk')), self.a1)