I am using the latest available version of django-mongodb-backend 5.1.0b1, and my data contains quite a few lists with objetcs in there. So it means I cannot use EmbeddedModelField, I need to use ArrayField with base_field=models.JSONField
I get json dump errors id these inner objects have something python json cannot serialize (ObjectIds but also datetimes)
This is in django_mongodb_backend.operations.DatabaseOperations.convert_jsonfield_value
on line 150.
This call uses a plain json.dumps and we cannot give it an encoder.
How come all these bson fields are not handled in there?
My embedded documents load fine if they are in EmbeddedModelField but the document says I cannot use that in ArrayField.
Anyone has faced the same problem? Any advised solution? It’s quite deep in the code, somewhere I cannot simply override.
Good suggestion. I believe this is more a bug fix request than a feature request: the connector crashes when a jsonfield (an embedded document any non-straight matched BSON to JSON datatype, like ObjectID or datetime)
Even nicer would be to have EmbeddedModelFields in ArrayField
from django.test import TestCase
from pymongo import MongoClient
import datetime
from asmorm.models import Observation
class ObservationTestCase(TestCase):
def test_observation(self):
Observation.objects.create()
observation = Observation.objects.all()[0]
self.assertIsNotNone(observation)
client = MongoClient("mongodb://localhost/")
db = client["test_demo"]
db.asmorm_observation.update_one(
{"_id": observation.pk},
{
"$set": {
"history": [
{"update": datetime.datetime.now(datetime.UTC), "value": "1"}
]
}
},
)
observation.refresh_from_db()
print(observation.history)
in tests.py
And this
import django_mongodb_backend
from django.db import models
from django_mongodb_backend.fields import ArrayField
class Observation(models.Model):
_if = django_mongodb_backend.fields.ObjectIdAutoField(primary_key=True)
history = ArrayField(base_field=models.JSONField(), default=list)