1
0
mirror of https://github.com/django/django.git synced 2025-06-13 07:29:13 +00:00

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>
This commit is contained in:
Adam Johnson 2025-05-21 15:16:12 +01:00 committed by Sarah Boyce
parent d2732c30af
commit b8e5a8a9a2
3 changed files with 25 additions and 6 deletions

View File

@ -95,7 +95,7 @@ class Aggregate(Func):
raise TypeError(f"{self.__class__.__name__} does not allow default.")
self.distinct = distinct
self.filter = filter and AggregateFilter(filter)
self.filter = None if filter is None else AggregateFilter(filter)
self.default = default
self.order_by = AggregateOrderBy.from_param(
f"{self.__class__.__name__}.order_by", order_by
@ -120,11 +120,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
)
c.order_by = (
c.order_by.resolve_expression(query, allow_joins, reuse, summarize)
if c.order_by

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(