Skip to content

Commit 179f2bb

Browse files
committed
Support encoder and decoder in jsonfield.
1 parent 1d3afa5 commit 179f2bb

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

django_mongodb/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def _connect(self):
140140
self.connection = MongoClient(
141141
host=settings_dict["HOST"] or None,
142142
port=int(settings_dict["PORT"] or 27017),
143-
uuidRepresentation="standard",
144143
**options,
145144
)
146145
db_name = settings_dict["NAME"]

django_mongodb/features.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
4646
"lookup.tests.LookupTests.test_pattern_lookups_with_substr",
4747
# Querying ObjectID with string doesn't work.
4848
"lookup.tests.LookupTests.test_lookup_int_as_str",
49-
"model_fields.test_jsonfield.TestQuerying.test_icontains"
49+
"model_fields.test_jsonfield.TestQuerying.test_icontains",
5050
# MongoDB gives the wrong result of log(number, base) when base is a
5151
# fractional Decimal: https://jira.mongodb.org/browse/SERVER-91223
5252
"db_functions.math.test_log.LogTests.test_decimal",
@@ -359,10 +359,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
359359
"db_functions.comparison.test_cast.CastTests.test_cast_from_python_to_datetime",
360360
"db_functions.comparison.test_cast.CastTests.test_cast_to_duration",
361361
},
362-
"Mongodb support uuid as a type in json fields.": {
363-
"model_fields.test_jsonfield.JSONFieldTests.test_invalid_value",
364-
"model_fields.test_jsonfield.JSONFieldTests.test_db_check_constraints",
365-
},
366362
"Mongodb's Null behaviour is different from sql's": {
367363
"model_fields.test_jsonfield.TestQuerying.test_expression_wrapper_key_transform",
368364
"model_fields.test_jsonfield.TestSaveLoad.test_json_null_different_from_sql_null",

django_mongodb/fields/json_field.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from django.db import NotSupportedError
24
from django.db.models import JSONField
35
from django.db.models.fields.json import (
@@ -31,13 +33,13 @@ def data_contains(self, compiler, connection): # noqa: ARG001
3133

3234
def from_db_value(self, value, expression, connection):
3335
"""
34-
MongoDB does not need to change the json value. It is stored as it is.
36+
Transforms the value retrieved from the database into a string if the database
37+
vendor is MongoDB. This is necessary because MongoDB saves the data as a
38+
dictionary, and converting it to a JSON string allows for proper decoding.
3539
"""
36-
return (
37-
value
38-
if connection.vendor == "mongodb"
39-
else _from_db_value(self, value, expression, connection)
40-
)
40+
if connection.vendor == "mongodb":
41+
value = json.dumps(value)
42+
return _from_db_value(self, value, expression, connection)
4143

4244

4345
def has_key_lookup(self, compiler, connection):
@@ -62,7 +64,7 @@ def has_key_lookup(self, compiler, connection):
6264
return {self.mongo_operator: keys}
6365

6466

65-
__process_rhs = JSONExact.process_rhs
67+
_process_rhs = JSONExact.process_rhs
6668

6769

6870
def json_process_rhs(self, compiler, connection):
@@ -73,7 +75,7 @@ def json_process_rhs(self, compiler, connection):
7375
return (
7476
super(JSONExact, self).process_rhs(compiler, connection)
7577
if connection.vendor == "mongodb"
76-
else __process_rhs(self, compiler, connection)
78+
else _process_rhs(self, compiler, connection)
7779
)
7880

7981

django_mongodb/operations.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import datetime
2+
import json
23
import uuid
34

45
from bson.decimal128 import Decimal128
56
from django.conf import settings
7+
from django.db import DataError
68
from django.db.backends.base.operations import BaseDatabaseOperations
79
from django.utils import timezone
810
from django.utils.regex_helper import _lazy_re_compile
@@ -37,7 +39,10 @@ def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
3739
return Decimal128(value)
3840

3941
def adapt_json_value(self, value, encoder):
40-
return value
42+
try:
43+
return json.loads(json.dumps(value, cls=encoder))
44+
except json.decoder.JSONDecodeError as e:
45+
raise DataError from e
4146

4247
def adapt_timefield_value(self, value):
4348
"""Store TimeField as datetime."""

0 commit comments

Comments
 (0)