Skip to content

Commit 5e31693

Browse files
committed
add f.get_db_prep_value
1 parent 9fb0d28 commit 5e31693

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

django_mongodb_backend/fields/embedded_model.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,19 @@ def formfield(self, **kwargs):
156156

157157
@EmbeddedModelField.register_lookup
158158
class EMFExact(lookups.Exact):
159-
def model_to_dict(self, instance):
159+
def model_to_dict(self, instance, connection):
160160
"""
161161
Return a dict containing the data in a model instance, as well as a
162162
dict containing the data for any embedded model fields.
163163
"""
164164
data = {}
165165
emf_data = {}
166166
for f in instance._meta.concrete_fields:
167-
value = f.value_from_object(instance)
167+
value = f.get_db_prep_value(f.value_from_object(instance), connection)
168168
if isinstance(f, EmbeddedModelField):
169-
emf_data[f.name] = self.model_to_dict(value) if value is not None else (None, {})
169+
emf_data[f.name] = (
170+
self.model_to_dict(value, connection) if value is not None else (None, {})
171+
)
170172
continue
171173
# Unless explicitly set, primary keys aren't included in embedded
172174
# models.
@@ -202,7 +204,7 @@ def as_mql(self, compiler, connection):
202204
and isinstance(self.lhs.ref_field, EmbeddedModelField)
203205
):
204206
if isinstance(value, models.Model):
205-
value, emf_data = self.model_to_dict(value)
207+
value, emf_data = self.model_to_dict(value, connection)
206208
# Get conditions for any nested EmbeddedModelFields.
207209
conditions = self.get_conditions({lhs_mql: (value, emf_data)})
208210
return {"$and": conditions}

tests/model_fields_/test_embedded_model.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,12 @@ def test_exact_with_model_ignores_key_order(self):
147147
# Due to the possibility of schema changes or the reordering of a
148148
# model's fields, a lookup must work if an embedded document has its
149149
# keys in a different order than what's declared on the embedded model.
150-
connection.get_collection("model_fields__holder").insert_one(
151-
{
152-
"data": {
153-
"auto_now": None,
154-
"auto_now_add": None,
155-
"json_value": None,
156-
"integer": 100,
157-
}
158-
}
159-
)
150+
data = {}
151+
for field in reversed(Data._meta.fields):
152+
data[field.name] = None
153+
del data["id"]
154+
data["integer"] = 100
155+
connection.get_collection("model_fields__holder").insert_one({"data": data})
160156
self.assertEqual(Holder.objects.filter(data=Data(integer=100)).get().data.integer, 100)
161157

162158
def test_exact_with_nested_model(self):

0 commit comments

Comments
 (0)