summaryrefslogtreecommitdiff
path: root/django/contrib
diff options
context:
space:
mode:
authorNils VAN ZUIJLEN <nils.van-zuijlen@mailo.com>2023-02-06 22:46:44 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-07 14:06:08 +0100
commite8a39da396b3ed8e469f569e4e865a54ae5dad0b (patch)
tree178884a9895c59698cf2cc2584516f6c3ed88bd7 /django/contrib
parent714d59d57f53fbbf1c0c2520bc3c981597cd2239 (diff)
[4.2.x] Fixed #34285 -- Fixed index/slice lookups on filtered aggregates with ArrayField.
Thanks Simon Charette for the review. Backport of ae1fe72e9b1f5fe3b05e5b670bd0c205cd305e71 from main
Diffstat (limited to 'django/contrib')
-rw-r--r--django/contrib/postgres/fields/array.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
index 8477dd9fff..c8e8e132e0 100644
--- a/django/contrib/postgres/fields/array.py
+++ b/django/contrib/postgres/fields/array.py
@@ -325,7 +325,9 @@ class IndexTransform(Transform):
def as_sql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
- return "%s[%%s]" % lhs, params + [self.index]
+ if not lhs.endswith("]"):
+ lhs = "(%s)" % lhs
+ return "%s[%%s]" % lhs, (*params, self.index)
@property
def output_field(self):
@@ -349,7 +351,9 @@ class SliceTransform(Transform):
def as_sql(self, compiler, connection):
lhs, params = compiler.compile(self.lhs)
- return "%s[%%s:%%s]" % lhs, params + [self.start, self.end]
+ if not lhs.endswith("]"):
+ lhs = "(%s)" % lhs
+ return "%s[%%s:%%s]" % lhs, (*params, self.start, self.end)
class SliceTransformFactory: