1
0
mirror of https://github.com/django/django.git synced 2025-06-03 18:49:12 +00:00

[5.2.x] Fixed #36404 -- Fixed Aggregate.filter using OuterRef.

Regression in a76035e925ff4e6d8676c65cb135c74b993b1039.
Thank you to Simon Charette for the review.

co-authored-by: Simon Charette <charette.s@gmail.com>

Backport of b8e5a8a9a2a767f584cbe89a878a42363706f939 from main.
This commit is contained in:
Adam Johnson 2025-05-21 16:16:12 +02:00 committed by Sarah Boyce
parent bd873e84be
commit c29e3092fd
3 changed files with 24 additions and 5 deletions

View File

@ -61,11 +61,6 @@ class Aggregate(Func):
):
# Aggregates are not allowed in UPDATE queries, so ignore for_save
c = super().resolve_expression(query, allow_joins, reuse, summarize)
c.filter = (
c.filter.resolve_expression(query, allow_joins, reuse, summarize)
if c.filter
else None
)
if summarize:
# Summarized aggregates cannot refer to summarized aggregates.
for ref in c.get_refs():

View File

@ -22,3 +22,6 @@ Bugfixes
* Fixed a regression in Django 5.2 where subclasses of ``RemoteUserMiddleware``
that had overridden ``process_request()`` were no longer supported
(:ticket:`36390`).
* Fixed a regression in Django 5.2 that caused a crash when using ``OuterRef``
in the ``filter`` argument of an ``Aggregate`` expression (:ticket:`36404`).

View File

@ -84,6 +84,10 @@ class FilteredAggregateTests(TestCase):
Author.objects.aggregate(age=agg)["age"], expected_result
)
def test_empty_filtered_aggregates(self):
agg = Count("pk", filter=Q())
self.assertEqual(Author.objects.aggregate(count=agg)["count"], 3)
def test_double_filtered_aggregates(self):
agg = Sum("age", filter=Q(Q(name="test2") & ~Q(name="test")))
self.assertEqual(Author.objects.aggregate(age=agg)["age"], 60)
@ -182,6 +186,23 @@ class FilteredAggregateTests(TestCase):
)
self.assertEqual(aggregate, {"max_rating": 4.5})
def test_filtered_aggregrate_ref_in_subquery_annotation(self):
aggs = (
Author.objects.annotate(
count=Subquery(
Book.objects.annotate(
weird_count=Count(
"pk",
filter=Q(pages=OuterRef("age")),
)
).values("weird_count")[:1]
),
)
.order_by("pk")
.aggregate(sum=Sum("count"))
)
self.assertEqual(aggs["sum"], 0)
def test_filtered_aggregate_on_exists(self):
aggregate = Book.objects.values("publisher").aggregate(
max_rating=Max(