Code

Ticket #7302: 7302.1.diff

File 7302.1.diff, 1.2 KB (added by isagalaev, 6 years ago)

Possible fix with tests

Line 
1Index: django/db/models/sql/query.py
2===================================================================
3--- django/db/models/sql/query.py       (revision 8740)
4+++ django/db/models/sql/query.py       (working copy)
5@@ -629,7 +629,7 @@
6                     result.append('%s %s' % (elt, order))
7             else:
8                 col, order = get_order_dir(field, asc)
9-                elt = qn(col)
10+                elt = qn2(col)
11                 if distinct and elt not in select_aliases:
12                     ordering_aliases.append(elt)
13                 result.append('%s %s' % (elt, order))
14Index: tests/modeltests/ordering/models.py
15===================================================================
16--- tests/modeltests/ordering/models.py (revision 8740)
17+++ tests/modeltests/ordering/models.py (working copy)
18@@ -77,4 +77,8 @@
19 # take the first two).
20 >>> Article.objects.all().reverse()[:2]
21 [<Article: Article 1>, <Article: Article 3>]
22+
23+# Ordering with 'extra' properly quotes aliases to not clash with SQL keywords
24+>>> Article.objects.extra(select={'order': 'pub_date'}, order_by=['order', 'headline'])
25+[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
26 """}