@@ -173,7 +173,7 @@ def model_to_dict(self, instance):
173
173
data [f .name ] = value
174
174
return data , emf_data
175
175
176
- def get_conditions (self , emf_data , prefix ):
176
+ def get_conditions (self , emf_data , prefix = None ):
177
177
"""
178
178
Recursively transform a dictionary of {"field_name": {<model_to_dict>}}
179
179
lookups into MQL. `prefix` tracks the string that must be appended to
@@ -182,7 +182,7 @@ def get_conditions(self, emf_data, prefix):
182
182
conditions = []
183
183
for k , v in emf_data .items ():
184
184
v , emf_data = v
185
- subprefix = f"{ prefix } .{ k } "
185
+ subprefix = f"{ prefix } .{ k } " if prefix else k
186
186
conditions += self .get_conditions (emf_data , subprefix )
187
187
if v is not None :
188
188
# Match all field of the EmbeddedModelField.
@@ -202,10 +202,8 @@ def as_mql(self, compiler, connection):
202
202
if isinstance (value , models .Model ):
203
203
value , emf_data = self .model_to_dict (value )
204
204
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 ()]
207
205
# Get conditions for any nested EmbeddedModelFields.
208
- conditions + = self .get_conditions (emf_data , prefix )
206
+ conditions = self .get_conditions ({ prefix : ( value , emf_data )} )
209
207
return {"$and" : conditions }
210
208
raise TypeError (
211
209
"An EmbeddedModelField must be queried using a model instance, got %s."
0 commit comments