diff options
Diffstat (limited to 'django/contrib/postgres/fields/array.py')
| -rw-r--r-- | django/contrib/postgres/fields/array.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py index 5f30ed1ab1..9c1bb96b61 100644 --- a/django/contrib/postgres/fields/array.py +++ b/django/contrib/postgres/fields/array.py @@ -4,7 +4,7 @@ from django.contrib.postgres import lookups from django.contrib.postgres.forms import SimpleArrayField from django.contrib.postgres.validators import ArrayMaxLengthValidator from django.core import checks, exceptions -from django.db.models import Field, IntegerField, Transform +from django.db.models import Field, Func, IntegerField, Transform, Value from django.db.models.fields.mixins import CheckFieldDefaultMixin from django.db.models.lookups import Exact, In from django.utils.translation import gettext_lazy as _ @@ -198,7 +198,22 @@ class ArrayField(CheckFieldDefaultMixin, Field): }) -class ArrayCastRHSMixin: +class ArrayRHSMixin: + def __init__(self, lhs, rhs): + if isinstance(rhs, (tuple, list)): + expressions = [] + for value in rhs: + if not hasattr(value, 'resolve_expression'): + field = lhs.output_field + value = Value(field.base_field.get_prep_value(value)) + expressions.append(value) + rhs = Func( + *expressions, + function='ARRAY', + template='%(function)s[%(expressions)s]', + ) + super().__init__(lhs, rhs) + def process_rhs(self, compiler, connection): rhs, rhs_params = super().process_rhs(compiler, connection) cast_type = self.lhs.output_field.cast_db_type(connection) @@ -206,22 +221,22 @@ class ArrayCastRHSMixin: @ArrayField.register_lookup -class ArrayContains(ArrayCastRHSMixin, lookups.DataContains): +class ArrayContains(ArrayRHSMixin, lookups.DataContains): pass @ArrayField.register_lookup -class ArrayContainedBy(ArrayCastRHSMixin, lookups.ContainedBy): +class ArrayContainedBy(ArrayRHSMixin, lookups.ContainedBy): pass @ArrayField.register_lookup -class ArrayExact(ArrayCastRHSMixin, Exact): +class ArrayExact(ArrayRHSMixin, Exact): pass @ArrayField.register_lookup -class ArrayOverlap(ArrayCastRHSMixin, lookups.Overlap): +class ArrayOverlap(ArrayRHSMixin, lookups.Overlap): pass |
