From a6ecd5dbb34249f756a337c359eef1e8d78dc01e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 26 Apr 2014 03:34:20 -0400 Subject: [1.7.x] Fixed #19195 -- Allow explicit ordering by a relation `_id` field. Thanks to chrisedgemon for the report and shaib, akaariai and timgraham for the review. Backport of 24ec9538b7 from master --- django/db/models/sql/compiler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 09b1f7d4d8..6792824ecd 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -464,8 +464,9 @@ class SQLCompiler(object): field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias) # If we get to this point and the field is a relation to another model, - # append the default ordering for that model. - if field.rel and path and opts.ordering: + # append the default ordering for that model unless the attribute name + # of the field is specified. + if field.rel and path and opts.ordering and name != field.attname: # Firstly, avoid infinite loops. if not already_seen: already_seen = set() -- cgit v1.3