diff options
Diffstat (limited to 'django/contrib/postgres/fields/array.py')
| -rw-r--r-- | django/contrib/postgres/fields/array.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py index c8e8e132e0..4171af82f9 100644 --- a/django/contrib/postgres/fields/array.py +++ b/django/contrib/postgres/fields/array.py @@ -234,6 +234,12 @@ class ArrayField(CheckFieldDefaultMixin, Field): } ) + def slice_expression(self, expression, start, length): + # If length is not provided, don't specify an end to slice to the end + # of the array. + end = None if length is None else start + length - 1 + return SliceTransform(start, end, expression) + class ArrayRHSMixin: def __init__(self, lhs, rhs): @@ -351,9 +357,11 @@ class SliceTransform(Transform): def as_sql(self, compiler, connection): lhs, params = compiler.compile(self.lhs) - if not lhs.endswith("]"): - lhs = "(%s)" % lhs - return "%s[%%s:%%s]" % lhs, (*params, self.start, self.end) + # self.start is set to 1 if slice start is not provided. + if self.end is None: + return f"({lhs})[%s:]", (*params, self.start) + else: + return f"({lhs})[%s:%s]", (*params, self.start, self.end) class SliceTransformFactory: |
