Skip to content

Commit

Permalink
Fixed #30011 -- Fixed queries that reuse filtered aggregates.
Browse files Browse the repository at this point in the history
Thanks Taqi Abbas and Raphael Kimmig for the report.
  • Loading branch information
charettes authored and timgraham committed Dec 6, 2018
1 parent 88619e6 commit 53269bc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_source_fields(self):
def get_source_expressions(self):
source_expressions = super().get_source_expressions()
if self.filter:
source_expressions += [self.filter]
return source_expressions + [self.filter]
return source_expressions

def set_source_expressions(self, exprs):
Expand Down
8 changes: 8 additions & 0 deletions tests/aggregation/test_filter_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ def test_sum_star_exception(self):
msg = 'Star cannot be used with filter. Please specify a field.'
with self.assertRaisesMessage(ValueError, msg):
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)

0 comments on commit 53269bc

Please sign in to comment.