summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/sql/compiler.py2
-rw-r--r--tests/regressiontests/aggregation_regress/tests.py12
2 files changed, 13 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index d464061f95..fb9674c366 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -327,7 +327,7 @@ class SQLCompiler(object):
continue
col, order = get_order_dir(field, asc)
if col in self.query.aggregate_select:
- result.append('%s %s' % (col, order))
+ result.append('%s %s' % (qn(col), order))
continue
if '.' in field:
# This came in through an extra(order_by=...) addition. Pass it
diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
index 822785b7a5..44c79c36c1 100644
--- a/tests/regressiontests/aggregation_regress/tests.py
+++ b/tests/regressiontests/aggregation_regress/tests.py
@@ -768,6 +768,18 @@ class AggregationTests(TestCase):
attrgetter("name")
)
+ def test_quoting_aggregate_order_by(self):
+ qs = Book.objects.filter(
+ name="Python Web Development with Django"
+ ).annotate(
+ authorCount=Count("authors")
+ ).order_by("authorCount")
+ self.assertQuerysetEqual(
+ qs, [
+ ("Python Web Development with Django", 3),
+ ],
+ lambda b: (b.name, b.authorCount)
+ )
if run_stddev_tests():
def test_stddev(self):