Skip to content

Commit dfc1317

Browse files
committed
Add as_path paramter
1 parent fe82dea commit dfc1317

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

django_mongodb_backend/expressions/builtins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def case(self, compiler, connection):
5353
}
5454

5555

56-
def col(self, compiler, connection): # noqa: ARG001
56+
def col(self, compiler, connection, as_path=False): # noqa: ARG001
5757
# If the column is part of a subquery and belongs to one of the parent
5858
# queries, it will be stored for reference using $let in a $lookup stage.
5959
# If the query is built with `alias_cols=False`, treat the column as
@@ -71,7 +71,7 @@ def col(self, compiler, connection): # noqa: ARG001
7171
# Add the column's collection's alias for columns in joined collections.
7272
has_alias = self.alias and self.alias != compiler.collection_name
7373
prefix = f"{self.alias}." if has_alias else ""
74-
return f"${prefix}{self.target.column}"
74+
return f"{prefix}{self.target.column}" if as_path else f"${prefix}{self.target.column}"
7575

7676

7777
def col_pairs(self, compiler, connection):

django_mongodb_backend/fields/embedded_model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,16 @@ def get_transform(self, name):
184184
f"{suggestion}"
185185
)
186186

187-
def as_mql(self, compiler, connection):
187+
def as_mql(self, compiler, connection, as_path=False):
188188
previous = self
189189
key_transforms = []
190190
while isinstance(previous, KeyTransform):
191191
key_transforms.insert(0, previous.key_name)
192192
previous = previous.lhs
193+
if as_path:
194+
mql = previous.as_mql(compiler, connection, as_path=True)
195+
mql_path = ".".join(key_transforms)
196+
return f"{mql}.{mql_path}"
193197
mql = previous.as_mql(compiler, connection)
194198
for key in key_transforms:
195199
mql = {"$getField": {"input": mql, "field": key}}

0 commit comments

Comments
 (0)