summaryrefslogtreecommitdiff
path: root/django/db/models
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-10-15 21:59:15 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-10-16 05:37:30 +0200
commit3b4a571275d967512866012955eb0b3ae486d63c (patch)
tree36a2db000304ab32b3a287d11449284c30245e91 /django/db/models
parent73b2c63127297e214d3bfdc8d9a96837691fc2a0 (diff)
Fixed #34798 -- Fixed QuerySet.aggregate() crash when referencing expressions containing subqueries.
Regression in 59bea9efd2768102fc9d3aedda469502c218e9b7, complements e5c844d6f2a4ac6ae674d741b5f1fa2a688cedf4. Refs #28477, #34551. Thanks Haldun Komsuoglu for the report.
Diffstat (limited to 'django/db/models')
-rw-r--r--django/db/models/expressions.py7
-rw-r--r--django/db/models/sql/query.py2
2 files changed, 8 insertions, 1 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index 181a04493f..9aa78a281f 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -256,6 +256,13 @@ class BaseExpression:
for expr in self.get_source_expressions()
)
+ @cached_property
+ def contains_subquery(self):
+ return any(
+ expr and (getattr(expr, "subquery", False) or expr.contains_subquery)
+ for expr in self.get_source_expressions()
+ )
+
def resolve_expression(
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
):
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index a7839ccb4d..4690fd304d 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -452,7 +452,7 @@ class Query(BaseExpression):
# members of `aggregates` to resolve against each others.
self.append_annotation_mask([alias])
refs_subquery |= any(
- getattr(self.annotations[ref], "subquery", False)
+ getattr(self.annotations[ref], "contains_subquery", False)
for ref in aggregate.get_refs()
)
refs_window |= any(