Skip to content

Commit e7088d1

Browse files
committed
add database converters for ArrayField
1 parent bc48e54 commit e7088d1

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9292
"model_fields_.test_arrayfield.TestQuerying.test_exact_with_expression",
9393
# int() argument must be a string, a bytes-like object or a real number, not 'list'
9494
"model_fields_.test_arrayfield.TestQuerying.test_index_annotation",
95-
# the JSON object must be str, bytes or bytearray, not dict
96-
"model_fields_.test_arrayfield.TestSaveLoad.test_other_array_types",
9795
# Wrong results
9896
"model_fields_.test_arrayfield.TestQuerying.test_exact",
9997
"model_fields_.test_arrayfield.TestQuerying.test_gt",
@@ -111,8 +109,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
111109
"model_fields_.test_arrayfield.TestQuerying.test_slice",
112110
"model_fields_.test_arrayfield.TestQuerying.test_slice_annotation",
113111
"model_fields_.test_arrayfield.TestQuerying.test_usage_in_subquery",
114-
# database converters not applied
115-
"model_fields_.test_arrayfield.TestSaveLoad.test_dates",
116112
}
117113
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
118114
_django_test_expected_failures_bitwise = {

django_mongodb/operations.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.db import DataError
1010
from django.db.backends.base.operations import BaseDatabaseOperations
1111
from django.db.models import TextField
12-
from django.db.models.expressions import Combinable
12+
from django.db.models.expressions import Combinable, Expression
1313
from django.db.models.functions import Cast
1414
from django.utils import timezone
1515
from django.utils.regex_helper import _lazy_re_compile
@@ -77,10 +77,26 @@ def adapt_timefield_value(self, value):
7777
raise ValueError("MongoDB backend does not support timezone-aware times.")
7878
return datetime.datetime.combine(datetime.datetime.min.date(), value)
7979

80+
def _get_arrayfield_converter(self, converter, *args, **kwargs):
81+
# Return a database converter that can be applied to a list of values.
82+
def convert_value(value, expression, connection):
83+
return [converter(x, expression, connection) for x in value]
84+
85+
return convert_value
86+
8087
def get_db_converters(self, expression):
8188
converters = super().get_db_converters(expression)
8289
internal_type = expression.output_field.get_internal_type()
83-
if internal_type == "DateField":
90+
if internal_type == "ArrayField":
91+
converters.extend(
92+
[
93+
self._get_arrayfield_converter(converter)
94+
for converter in self.get_db_converters(
95+
Expression(output_field=expression.output_field.base_field)
96+
)
97+
]
98+
)
99+
elif internal_type == "DateField":
84100
converters.append(self.convert_datefield_value)
85101
elif internal_type == "DateTimeField":
86102
if settings.USE_TZ:

0 commit comments

Comments
 (0)