From 80287f1e8a554290faa6a0c31113a9f4eecf7b79 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 26 Jan 2011 03:48:15 +0000 Subject: [PATCH] Fixed #13159 -- properly quote aggregates in order_by. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15318 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/compiler.py | 2 +- tests/regressiontests/aggregation_regress/tests.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index ba707d4140..d425c8b32b 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -330,7 +330,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 ad79923a8a..67220b7c77 100644 --- a/tests/regressiontests/aggregation_regress/tests.py +++ b/tests/regressiontests/aggregation_regress/tests.py @@ -747,6 +747,19 @@ 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) + ) + @skipUnlessDBFeature('supports_stddev') def test_stddev(self): self.assertEqual(