Skip to content

Commit c8a1c31

Browse files
committed
Refactor.
1 parent c8baa5a commit c8a1c31

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

django_mongodb_backend/fields/embedded_model.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,25 @@ def get_transform(self, name):
198198
f"{suggestion}"
199199
)
200200

201-
def as_mql_expr(self, compiler, connection):
201+
def _get_target_path(self):
202202
previous = self
203203
columns = []
204204
while isinstance(previous, EmbeddedModelTransform):
205205
columns.insert(0, previous.field.column)
206206
previous = previous.lhs
207-
mql = previous.as_mql(compiler, connection)
208-
for column in columns:
209-
mql = {"$getField": {"input": mql, "field": column}}
207+
return columns, previous
208+
209+
def as_mql_expr(self, compiler, connection):
210+
columns, parent_field = self._get_target_path()
211+
mql = parent_field.as_mql(compiler, connection)
212+
for key in columns:
213+
mql = {"$getField": {"input": mql, "field": key}}
210214
return mql
211215

212216
def as_mql_path(self, compiler, connection):
213-
previous = self
214-
key_transforms = []
215-
while isinstance(previous, EmbeddedModelTransform):
216-
key_transforms.insert(0, previous.key_name)
217-
previous = previous.lhs
218-
mql = previous.as_mql(compiler, connection, as_path=True)
219-
mql_path = ".".join(key_transforms)
217+
columns, parent_field = self._get_target_path()
218+
mql = parent_field.as_mql(compiler, connection, as_path=True)
219+
mql_path = ".".join(columns)
220220
return f"{mql}.{mql_path}"
221221

222222
@property

0 commit comments

Comments
 (0)