diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 7d24701438..491e5b2f5b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -518,14 +518,14 @@ class Query(object): # If we get to this point and the field is a relation to another model, # append the default ordering for that model. - if len(joins) > 1 and opts.ordering: + if field.rel and len(joins) > 1 and opts.ordering: # Firstly, avoid infinite loops. if not already_seen: - already_seen = {} + already_seen = set() join_tuple = tuple([tuple(j) for j in joins]) if join_tuple in already_seen: raise FieldError('Infinite loop caused by ordering.') - already_seen[join_tuple] = True + already_seen.add(join_tuple) results = [] for item in opts.ordering: diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 17476217cd..7185558f45 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -407,6 +407,11 @@ Traceback (most recent call last): ... FieldError: Infinite loop caused by ordering. +# ... but you can still order in a non-recursive fashion amongst linked fields +# (the previous test failed because the default ordering was recursive). +>>> LoopX.objects.all().order_by('y__x__id') +[] + # If the remote model does not have a default ordering, we order by its 'id' # field. >>> Item.objects.order_by('creator', 'name')