Skip to content

Commit 9cfe204

Browse files
WaVEVtimgraham
authored andcommitted
add support for Transforms in QuerySet.values()/values_list()
1 parent 04ecfa0 commit 9cfe204

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ jobs:
7777
db_functions
7878
empty
7979
expressions.tests.ExpressionOperatorTests
80+
expressions.tests.FieldTransformTests.test_transform_in_values
8081
expressions.tests.NegatedExpressionTests
8182
expressions_case
8283
defer

django_mongodb/compiler.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,19 @@ def get_columns(self):
153153
columns = (
154154
self.get_default_columns(select_mask) if self.query.default_cols else self.query.select
155155
)
156-
return tuple((column.target.column, column) for column in columns) + tuple(
157-
self.query.annotation_select.items()
158-
)
156+
annotation_idx = 1
157+
result = []
158+
for column in columns:
159+
if hasattr(column, "target"):
160+
# column is a Col.
161+
target = column.target.column
162+
else:
163+
# column is a Transform in values()/values_list() that needs a
164+
# name for $proj.
165+
target = f"__annotation{annotation_idx}"
166+
annotation_idx += 1
167+
result.append((target, column))
168+
return tuple(result) + tuple(self.query.annotation_select.items())
159169

160170
def _get_ordering(self):
161171
"""

0 commit comments

Comments
 (0)