Skip to content

add support for Transforms in QuerySet.values()/values_list() #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
db_functions
empty
expressions.tests.ExpressionOperatorTests
expressions.tests.FieldTransformTests.test_transform_in_values
expressions.tests.NegatedExpressionTests
expressions_case
defer
Expand Down
16 changes: 13 additions & 3 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,19 @@ def get_columns(self):
columns = (
self.get_default_columns(select_mask) if self.query.default_cols else self.query.select
)
return tuple((column.target.column, column) for column in columns) + tuple(
self.query.annotation_select.items()
)
annotation_idx = 1
result = []
for column in columns:
if hasattr(column, "target"):
# column is a Col.
target = column.target.column
else:
# column is a Transform in values()/values_list() that needs a
# name for $proj.
target = f"__annotation{annotation_idx}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just changed this. I added an _ between the annotation and the number. But it does not matter, I will keep the one that goes in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version is consistent with Django's internal aliasing __col{index}.

annotation_idx += 1
result.append((target, column))
return tuple(result) + tuple(self.query.annotation_select.items())

def _get_ordering(self):
"""
Expand Down