Skip to content

Fix database converter crash when loading models with a null ArrayField value #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions django_mongodb_backend/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def adapt_timefield_value(self, 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):
if value is None:
return None
return [converter(x, expression, connection) for x in value]

return convert_value
Expand Down
8 changes: 8 additions & 0 deletions docs/source/releases/5.1.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Django MongoDB Backend 5.1.x
============================

5.1.0 beta 4
============

*Unreleased*

- Fixed crash when loading models with a null value for ``ArrayField``\s where
the ``base_field`` uses a database converter.

5.1.0 beta 3
============

Expand Down
11 changes: 11 additions & 0 deletions docs/source/releases/5.2.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
Django MongoDB Backend 5.2.x
============================

5.2.0 beta 1
============

*Unreleased*

Bug fixes
---------

- Fixed crash when loading models with a null value for ``ArrayField``\s where
the ``base_field`` uses a database converter.

5.2.0 beta 0
============

Expand Down
7 changes: 6 additions & 1 deletion tests/model_fields_/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ class NestedIntegerArrayModel(models.Model):
class OtherTypesArrayModel(models.Model):
ips = ArrayField(models.GenericIPAddressField(), default=list)
uuids = ArrayField(models.UUIDField(), default=list)
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2), default=list)
decimals = ArrayField(
models.DecimalField(max_digits=5, decimal_places=2),
default=list,
null=True,
blank=True,
)
tags = ArrayField(TagField(), blank=True, null=True)
json = ArrayField(models.JSONField(default=dict), default=list)

Expand Down
3 changes: 2 additions & 1 deletion tests/model_fields_/test_arrayfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ def test_null_from_db_value_handling(self):
instance = OtherTypesArrayModel.objects.create(
ips=["192.168.0.1", "::1"],
uuids=[uuid.uuid4()],
decimals=[decimal.Decimal(1.25), 1.75],
decimals=None,
tags=None,
)
instance.refresh_from_db()
self.assertIsNone(instance.decimals)
self.assertIsNone(instance.tags)
self.assertEqual(instance.json, [])

Expand Down
Loading