Skip to content

Commit 3b230bf

Browse files
committed
Emanuel's suggestion
1 parent f62ae73 commit 3b230bf

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

django_mongodb_backend/fields/embedded_model.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def model_to_dict(self, instance):
173173
data[f.name] = value
174174
return data, emf_data
175175

176-
def get_conditions(self, emf_data, prefix):
176+
def get_conditions(self, emf_data, prefix=None):
177177
"""
178178
Recursively transform a dictionary of {"field_name": {<model_to_dict>}}
179179
lookups into MQL. `prefix` tracks the string that must be appended to
@@ -182,7 +182,7 @@ def get_conditions(self, emf_data, prefix):
182182
conditions = []
183183
for k, v in emf_data.items():
184184
v, emf_data = v
185-
subprefix = f"{prefix}.{k}"
185+
subprefix = f"{prefix}.{k}" if prefix else k
186186
conditions += self.get_conditions(emf_data, subprefix)
187187
if v is not None:
188188
# Match all field of the EmbeddedModelField.
@@ -202,10 +202,8 @@ def as_mql(self, compiler, connection):
202202
if isinstance(value, models.Model):
203203
value, emf_data = self.model_to_dict(value)
204204
prefix = self.lhs.as_mql(compiler, connection)
205-
# Get conditions for top-level EmbeddedModelField.
206-
conditions = [{"$eq": [f"{prefix}.{k}", v]} for k, v in value.items()]
207205
# Get conditions for any nested EmbeddedModelFields.
208-
conditions += self.get_conditions(emf_data, prefix)
206+
conditions = self.get_conditions({prefix: (value, emf_data)})
209207
return {"$and": conditions}
210208
raise TypeError(
211209
"An EmbeddedModelField must be queried using a model instance, got %s."

0 commit comments

Comments
 (0)