mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +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:
parent
bd873e84be
commit
c29e3092fd
@ -61,11 +61,6 @@ class Aggregate(Func):
|
|||||||
):
|
):
|
||||||
# Aggregates are not allowed in UPDATE queries, so ignore for_save
|
# Aggregates are not allowed in UPDATE queries, so ignore for_save
|
||||||
c = super().resolve_expression(query, allow_joins, reuse, summarize)
|
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:
|
if summarize:
|
||||||
# Summarized aggregates cannot refer to summarized aggregates.
|
# Summarized aggregates cannot refer to summarized aggregates.
|
||||||
for ref in c.get_refs():
|
for ref in c.get_refs():
|
||||||
|
@ -22,3 +22,6 @@ Bugfixes
|
|||||||
* Fixed a regression in Django 5.2 where subclasses of ``RemoteUserMiddleware``
|
* Fixed a regression in Django 5.2 where subclasses of ``RemoteUserMiddleware``
|
||||||
that had overridden ``process_request()`` were no longer supported
|
that had overridden ``process_request()`` were no longer supported
|
||||||
(:ticket:`36390`).
|
(: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`).
|
||||||
|
@ -84,6 +84,10 @@ class FilteredAggregateTests(TestCase):
|
|||||||
Author.objects.aggregate(age=agg)["age"], expected_result
|
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):
|
def test_double_filtered_aggregates(self):
|
||||||
agg = Sum("age", filter=Q(Q(name="test2") & ~Q(name="test")))
|
agg = Sum("age", filter=Q(Q(name="test2") & ~Q(name="test")))
|
||||||
self.assertEqual(Author.objects.aggregate(age=agg)["age"], 60)
|
self.assertEqual(Author.objects.aggregate(age=agg)["age"], 60)
|
||||||
@ -182,6 +186,23 @@ class FilteredAggregateTests(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(aggregate, {"max_rating": 4.5})
|
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):
|
def test_filtered_aggregate_on_exists(self):
|
||||||
aggregate = Book.objects.values("publisher").aggregate(
|
aggregate = Book.objects.values("publisher").aggregate(
|
||||||
max_rating=Max(
|
max_rating=Max(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user