Skip to content

Commit 3885833

Browse files
committed
Fix database converter crash when loading models with a null ArrayField value
1 parent 223a271 commit 3885833

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

django_mongodb_backend/operations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def adapt_timefield_value(self, value):
8080
def _get_arrayfield_converter(self, converter, *args, **kwargs):
8181
# Return a database converter that can be applied to a list of values.
8282
def convert_value(value, expression, connection):
83+
if value is None:
84+
return None
8385
return [converter(x, expression, connection) for x in value]
8486

8587
return convert_value

docs/source/releases/5.1.x.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Django MongoDB Backend 5.1.x
33
============================
44

5+
5.1.0 beta 4
6+
============
7+
8+
*Unreleased*
9+
10+
- Fixed crash when loading models with a null value for ``ArrayField``\s where
11+
the ``base_field`` uses a database converter.
12+
513
5.1.0 beta 3
614
============
715

docs/source/releases/5.2.x.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Django MongoDB Backend 5.2.x
33
============================
44

5+
5.2.0 beta 1
6+
============
7+
8+
*Unreleased*
9+
10+
Bug fixes
11+
---------
12+
13+
- Fixed crash when loading models with a null value for ``ArrayField``\s where
14+
the ``base_field`` uses a database converter.
15+
516
5.2.0 beta 0
617
============
718

tests/model_fields_/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ class NestedIntegerArrayModel(models.Model):
8080
class OtherTypesArrayModel(models.Model):
8181
ips = ArrayField(models.GenericIPAddressField(), default=list)
8282
uuids = ArrayField(models.UUIDField(), default=list)
83-
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2), default=list)
83+
decimals = ArrayField(
84+
models.DecimalField(max_digits=5, decimal_places=2),
85+
default=list,
86+
null=True,
87+
blank=True,
88+
)
8489
tags = ArrayField(TagField(), blank=True, null=True)
8590
json = ArrayField(models.JSONField(default=dict), default=list)
8691

tests/model_fields_/test_arrayfield.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ def test_null_from_db_value_handling(self):
195195
instance = OtherTypesArrayModel.objects.create(
196196
ips=["192.168.0.1", "::1"],
197197
uuids=[uuid.uuid4()],
198-
decimals=[decimal.Decimal(1.25), 1.75],
198+
decimals=None,
199199
tags=None,
200200
)
201201
instance.refresh_from_db()
202+
self.assertIsNone(instance.decimals)
202203
self.assertIsNone(instance.tags)
203204
self.assertEqual(instance.json, [])
204205

0 commit comments

Comments
 (0)