[1.7.x] Fixed #22466 -- ordering by reverse foreign key
Ordering by reverse foreign key was broken by custom lookups patch (commit20bab2cf9d
). Thanks to everybody who helped solving this issue. Special thanks to Trac alias takis for reporting this. Backport of3b7c66a3ac
from master
This commit is contained in:
parent
2a2d7db01f
commit
76979a257d
|
@ -1432,6 +1432,8 @@ class Query(object):
|
||||||
alias = self.join(
|
alias = self.join(
|
||||||
connection, reuse=reuse, nullable=nullable, join_field=join.join_field)
|
connection, reuse=reuse, nullable=nullable, join_field=join.join_field)
|
||||||
joins.append(alias)
|
joins.append(alias)
|
||||||
|
if hasattr(final_field, 'field'):
|
||||||
|
final_field = final_field.field
|
||||||
return final_field, targets, opts, joins, path
|
return final_field, targets, opts, joins, path
|
||||||
|
|
||||||
def trim_joins(self, targets, joins, path):
|
def trim_joins(self, targets, joins, path):
|
||||||
|
|
|
@ -1396,6 +1396,18 @@ class Queries4Tests(BaseQuerysetTest):
|
||||||
qs = Author.objects.order_by().order_by('name')
|
qs = Author.objects.order_by().order_by('name')
|
||||||
self.assertTrue('ORDER BY' in qs.query.get_compiler(qs.db).as_sql()[0])
|
self.assertTrue('ORDER BY' in qs.query.get_compiler(qs.db).as_sql()[0])
|
||||||
|
|
||||||
|
def test_order_by_reverse_fk(self):
|
||||||
|
# It is possible to order by reverse of foreign key, although that can lead
|
||||||
|
# to duplicate results.
|
||||||
|
c1 = SimpleCategory.objects.create(name="category1")
|
||||||
|
c2 = SimpleCategory.objects.create(name="category2")
|
||||||
|
CategoryItem.objects.create(category=c1)
|
||||||
|
CategoryItem.objects.create(category=c2)
|
||||||
|
CategoryItem.objects.create(category=c1)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
SimpleCategory.objects.order_by('categoryitem', 'pk'),
|
||||||
|
[c1, c2, c1], lambda x: x)
|
||||||
|
|
||||||
def test_ticket10181(self):
|
def test_ticket10181(self):
|
||||||
# Avoid raising an EmptyResultSet if an inner query is probably
|
# Avoid raising an EmptyResultSet if an inner query is probably
|
||||||
# empty (and hence, not executed).
|
# empty (and hence, not executed).
|
||||||
|
|
Loading…
Reference in New Issue