mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #30011 -- Fixed queries that reuse filtered aggregates.
Thanks Taqi Abbas and Raphael Kimmig for the report.
This commit is contained in:
parent
88619e6129
commit
53269bcaaf
@ -27,7 +27,7 @@ class Aggregate(Func):
|
||||
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):
|
||||
|
@ -79,3 +79,11 @@ class FilteredAggregateTests(TestCase):
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user