Fixed #31657 -- Fixed ordering by attnames of self-referential ForeignKeys.

This commit is contained in:
Hasan Ramezani 2020-06-07 16:50:47 +02:00 committed by Mariusz Felisiak
parent c1f7de8acc
commit 037a624120
2 changed files with 11 additions and 1 deletions

View File

@ -727,7 +727,12 @@ class SQLCompiler:
# If we get to this point and the field is a relation to another model,
# append the default ordering for that model unless it is the pk
# shortcut or the attribute name of the field that is specified.
if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name and name != 'pk':
if (
field.is_relation and
opts.ordering and
getattr(field, 'attname', None) != pieces[-1] and
name != 'pk'
):
# Firstly, avoid infinite loops.
already_seen = already_seen or set()
join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins)

View File

@ -353,6 +353,11 @@ class OrderingTests(TestCase):
['Article 2', 'Article 1'],
attrgetter('headline'),
)
self.assertQuerysetEqual(
Article.objects.filter(author__isnull=False).order_by('author__editor_id'),
['Article 1', 'Article 2'],
attrgetter('headline'),
)
def test_order_by_f_expression(self):
self.assertQuerysetEqual(