diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-06-07 16:50:47 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-06-10 12:05:31 +0200 |
| commit | 4385ef0119c2e800f44ae92e3215cc8e571f6e7d (patch) | |
| tree | 0d07d1cc603a2f9cbe5eea7f39943fd53d12692b /django | |
| parent | bdfad5731924e02aae01d799632e8fcdcbd588fd (diff) | |
[3.1.x] Fixed #31657 -- Fixed ordering by attnames of self-referential ForeignKeys.
Backport of 037a624120b676858be4b93c4d0adda45916fd2a from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/sql/compiler.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index e6e840d8ea..8eb40e94af 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -720,7 +720,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) |
