Skip to content

Commit fec0025

Browse files
committed
Fix: Json insert type, some filter approaches.
1 parent f43a27f commit fec0025

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

django_mongodb/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
check_django_compatability()
88

99
from .expressions import register_expressions # noqa: E402
10+
from .fields import load_fields # noqa: E402
1011
from .functions import register_functions # noqa: E402
1112
from .lookups import register_lookups # noqa: E402
1213
from .query import register_nodes # noqa: E402
@@ -15,3 +16,4 @@
1516
register_functions()
1617
register_lookups()
1718
register_nodes()
19+
load_fields()

django_mongodb/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def _connect(self):
146146
host=settings_dict["HOST"] or None,
147147
port=int(settings_dict["PORT"] or 27017),
148148
tz_aware=True,
149+
uuidRepresentation="standard",
149150
**options,
150151
)
151152
db_name = settings_dict["NAME"]

django_mongodb/features.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
66
supports_foreign_keys = False
77
supports_ignore_conflicts = False
88
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/8
9-
supports_json_field = False
9+
supports_json_field = True
1010
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/7
1111
supports_transactions = False
1212
uses_savepoints = False
@@ -292,4 +292,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
292292
"timezones.tests.NewDatabaseTests.test_aware_datetime_in_local_timezone_with_microsecond",
293293
"timezones.tests.NewDatabaseTests.test_naive_datetime_with_microsecond",
294294
},
295+
"Mongodb support uuid as a type in json fields.": {
296+
"model_fields.test_jsonfield.JSONFieldTests.test_invalid_value",
297+
"model_fields.test_jsonfield.JSONFieldTests.test_db_check_constraints",
298+
},
295299
}

django_mongodb/fields.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
from bson import ObjectId, errors
22
from django.core import exceptions
3+
from django.db.models import JSONField
34
from django.db.models.fields import AutoField, Field
5+
from django.db.models.fields.json import DataContains, JSONExact, KeyTransform
46
from django.utils.translation import gettext_lazy as _
57

8+
from .base import DatabaseWrapper
9+
from .query_utils import process_lhs, process_rhs
10+
611

712
class MongoAutoField(AutoField):
813
default_error_messages = {
@@ -32,3 +37,42 @@ def to_python(self, value):
3237
code="invalid",
3338
params={"value": value},
3439
) from None
40+
41+
42+
_from_db_value = JSONField.from_db_value
43+
44+
45+
def from_db_value(self, value, expression, connection):
46+
return (
47+
value
48+
if isinstance(connection, DatabaseWrapper)
49+
else _from_db_value(self, value, expression, connection)
50+
)
51+
52+
53+
def key_transform(self, compiler, connection):
54+
_, _, key_transforms = self.preprocess_lhs(compiler, connection)
55+
lhs_mql = process_lhs(self, compiler, connection)
56+
return ".".join([lhs_mql, *key_transforms])
57+
58+
59+
def data_contains(self, compiler, connection):
60+
lhs_mql = process_lhs(self, compiler, connection)
61+
value = process_rhs(self, compiler, connection)
62+
# rhs_mql = connection.operators[self.lookup_name](value)
63+
if not value:
64+
return {lhs_mql: {"$ne": None}}
65+
return {lhs_mql: {"$all": value}}
66+
67+
68+
def json_exact(self, compiler, connection):
69+
lhs_mql = process_lhs(self, compiler, connection)
70+
rhs_mql = process_rhs(self, compiler, connection)
71+
return {lhs_mql: {"$eq": rhs_mql}}
72+
73+
74+
def load_fields():
75+
JSONField.from_db_value = from_db_value
76+
DataContains.as_mql = data_contains
77+
KeyTransform.as_mql = key_transform
78+
JSONExact.as_mql = json_exact

django_mongodb/operations.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,6 @@ def _prep_lookup_value(self, value, field, field_kind, lookup):
122122
if field_kind == "DecimalField":
123123
value = self.adapt_decimalfield_value(value, field.max_digits, field.decimal_places)
124124
return value
125+
126+
def adapt_json_value(self, value, encoder):
127+
return value

0 commit comments

Comments
 (0)