Skip to content

Commit 128f515

Browse files
committed
fix QuerySet.annotate() with F() expression
1 parent 0ec6e8e commit 128f515

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
run: >
6969
python3 django_repo/tests/runtests.py --settings mongodb_settings -v 2
7070
annotations.tests.NonAggregateAnnotationTestCase.test_basic_annotation
71+
annotations.tests.NonAggregateAnnotationTestCase.test_basic_f_annotation
7172
auth_tests.test_models.UserManagerTestCase
7273
backends.base.test_base.DatabaseWrapperTests
7374
basic

django_mongodb/query.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ def get_cursor(self):
8888
fields = {}
8989
for name, expr in self.columns or []:
9090
try:
91-
fields[expr.target.column] = 1
91+
column = expr.target.column
9292
except AttributeError:
9393
# Generate the MQL for an annotation.
9494
fields[name] = expr.as_mql_agg(self.compiler, self.connection)
95+
else:
96+
# If name != column, then this is an annotatation referencing
97+
# another column.
98+
fields[name] = 1 if name == column else f"${column}"
9599
pipeline = []
96100
if self.mongo_query:
97101
pipeline.append({"$match": self.mongo_query})

0 commit comments

Comments
 (0)