Skip to content

Commit b83c33c

Browse files
committed
fix QuerySet.annotate() without values/value_list()
Also fix annotating with Value().
1 parent 9fb4ca5 commit b83c33c

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

.github/workflows/test-python.yml

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

django_mongodb/compiler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ def execute_sql(
2222
# QuerySet.count()
2323
if self.query.annotations == {"__count": Count("*")}:
2424
return [self.get_count()]
25+
# Specify columns if there are any annotations so that annotations are
26+
# computed via $project.
27+
columns = self.get_columns() if self.query.annotations else None
2528
try:
26-
query = self.build_query()
29+
query = self.build_query(columns)
2730
except EmptyResultSet:
2831
return None
2932
return query.fetch()

django_mongodb/expressions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ def value(self, compiler, connection): # noqa: ARG001
99
return self.value
1010

1111

12+
def value_agg(self, compiler, connection): # noqa: ARG001
13+
return {"$literal": self.value}
14+
15+
1216
def register_expressions():
1317
Col.as_mql = col
1418
Value.as_mql = value
19+
Value.as_mql_agg = value_agg

0 commit comments

Comments
 (0)