Skip to content

Commit bbe7acb

Browse files
committed
allow order_by() to reference annotations
1 parent f11bf9c commit bbe7acb

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

django_mongodb/compiler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.core.exceptions import EmptyResultSet, FullResultSet
1+
from django.core.exceptions import EmptyResultSet, FieldDoesNotExist, FullResultSet
22
from django.db import DatabaseError, IntegrityError, NotSupportedError
33
from django.db.models import NOT_PROVIDED, Count, Expression
44
from django.db.models.aggregates import Aggregate
@@ -199,7 +199,11 @@ def _get_ordering(self):
199199
if name == "pk":
200200
name = opts.pk.name
201201

202-
field_ordering.append((opts.get_field(name), ascending))
202+
try:
203+
column = opts.get_field(name).column
204+
except FieldDoesNotExist:
205+
column = name
206+
field_ordering.append((column, ascending))
203207
return field_ordering
204208

205209
def get_collection(self):

django_mongodb/features.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
3838
"lookup.tests.LookupQueryingTests.test_lookup_in_order_by",
3939
"ordering.tests.OrderingTests.test_default_ordering_by_f_expression",
4040
"ordering.tests.OrderingTests.test_default_ordering_does_not_affect_group_by",
41+
"ordering.tests.OrderingTests.test_order_by_constant_value",
4142
"ordering.tests.OrderingTests.test_order_by_expression_ref",
4243
"ordering.tests.OrderingTests.test_order_by_f_expression",
4344
"ordering.tests.OrderingTests.test_order_by_f_expression_duplicates",
@@ -61,6 +62,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6162
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
6263
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
6364
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
65+
# alias().order_by() doesn't work.
66+
"annotations.tests.AliasTests.test_order_by_alias",
6467
}
6568
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
6669
_django_test_expected_failures_bitwise = {
@@ -221,22 +224,15 @@ def django_test_expected_failures(self):
221224
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_f_expression_annotation_with_aggregation",
222225
"annotations.tests.NonAggregateAnnotationTestCase.test_full_expression_annotation_with_aggregation",
223226
"annotations.tests.NonAggregateAnnotationTestCase.test_grouping_by_q_expression_annotation",
227+
"annotations.tests.NonAggregateAnnotationTestCase.test_order_by_aggregate",
224228
"annotations.tests.NonAggregateAnnotationTestCase.test_q_expression_annotation_with_aggregation",
225229
"expressions_case.tests.CaseDocumentationExamples.test_conditional_aggregation_example",
230+
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_count",
226231
# Func not implemented.
227232
"annotations.tests.NonAggregateAnnotationTestCase.test_custom_functions",
228233
"annotations.tests.NonAggregateAnnotationTestCase.test_custom_functions_can_ref_other_functions",
229234
# BaseDatabaseOperations may require a format_for_duration_arithmetic().
230235
"annotations.tests.NonAggregateAnnotationTestCase.test_mixed_type_annotation_date_interval",
231-
# FieldDoesNotExist with ordering.
232-
"annotations.tests.AliasTests.test_order_by_alias",
233-
"annotations.tests.NonAggregateAnnotationTestCase.test_order_by_aggregate",
234-
"annotations.tests.NonAggregateAnnotationTestCase.test_order_by_annotation",
235-
"expressions.tests.NegatedExpressionTests.test_filter",
236-
"expressions_case.tests.CaseExpressionTests.test_annotate_values_not_in_order_by",
237-
"expressions_case.tests.CaseExpressionTests.test_order_by_conditional_implicit",
238-
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_count",
239-
"ordering.tests.OrderingTests.test_order_by_constant_value",
240236
},
241237
"Exists is not supported on MongoDB.": {
242238
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_exists_none_query",

django_mongodb/query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def order_by(self, ordering):
6464
Reorder query results or execution order. Called by compiler during
6565
query building.
6666
67-
`ordering` is a list with (field, ascending) tuples or a boolean --
67+
`ordering` is a list with (column, ascending) tuples or a boolean --
6868
use natural ordering, if any, when the argument is True and its reverse
6969
otherwise.
7070
"""
@@ -73,9 +73,9 @@ def order_by(self, ordering):
7373
if not ordering:
7474
self.ordering.append(("$natural", DESCENDING))
7575
else:
76-
for field, ascending in ordering:
76+
for column, ascending in ordering:
7777
direction = ASCENDING if ascending else DESCENDING
78-
self.ordering.append((field.column, direction))
78+
self.ordering.append((column, direction))
7979

8080
@wrap_database_errors
8181
def delete(self):

0 commit comments

Comments
 (0)