Skip to content

Commit

Permalink
add database converters for ArrayField
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Dec 11, 2024
1 parent bc48e54 commit e7088d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 0 additions & 4 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"model_fields_.test_arrayfield.TestQuerying.test_exact_with_expression",
# int() argument must be a string, a bytes-like object or a real number, not 'list'
"model_fields_.test_arrayfield.TestQuerying.test_index_annotation",
# the JSON object must be str, bytes or bytearray, not dict
"model_fields_.test_arrayfield.TestSaveLoad.test_other_array_types",
# Wrong results
"model_fields_.test_arrayfield.TestQuerying.test_exact",
"model_fields_.test_arrayfield.TestQuerying.test_gt",
Expand All @@ -111,8 +109,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"model_fields_.test_arrayfield.TestQuerying.test_slice",
"model_fields_.test_arrayfield.TestQuerying.test_slice_annotation",
"model_fields_.test_arrayfield.TestQuerying.test_usage_in_subquery",
# database converters not applied
"model_fields_.test_arrayfield.TestSaveLoad.test_dates",
}
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
_django_test_expected_failures_bitwise = {
Expand Down
20 changes: 18 additions & 2 deletions django_mongodb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.db import DataError
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models import TextField
from django.db.models.expressions import Combinable
from django.db.models.expressions import Combinable, Expression
from django.db.models.functions import Cast
from django.utils import timezone
from django.utils.regex_helper import _lazy_re_compile
Expand Down Expand Up @@ -77,10 +77,26 @@ def adapt_timefield_value(self, value):
raise ValueError("MongoDB backend does not support timezone-aware times.")
return datetime.datetime.combine(datetime.datetime.min.date(), value)

def _get_arrayfield_converter(self, converter, *args, **kwargs):
# Return a database converter that can be applied to a list of values.
def convert_value(value, expression, connection):
return [converter(x, expression, connection) for x in value]

return convert_value

def get_db_converters(self, expression):
converters = super().get_db_converters(expression)
internal_type = expression.output_field.get_internal_type()
if internal_type == "DateField":
if internal_type == "ArrayField":
converters.extend(
[
self._get_arrayfield_converter(converter)
for converter in self.get_db_converters(
Expression(output_field=expression.output_field.base_field)
)
]
)
elif internal_type == "DateField":
converters.append(self.convert_datefield_value)
elif internal_type == "DateTimeField":
if settings.USE_TZ:
Expand Down

0 comments on commit e7088d1

Please sign in to comment.