diff --git a/django/db/models/query.py b/django/db/models/query.py index 1a65286998..328ac2f3cd 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -234,12 +234,16 @@ class FieldFound(Exception): "Exception used to short circuit field-finding operations." pass -def find_field(name, field_list): +def find_field(name, field_list, use_accessor=False): """ Finds a field with a specific name in a list of field instances. Returns None if there are no matches, or several matches. """ - matches = [f for f in field_list if f.name == name] + + if use_accessor: + matches = [f for f in field_list if f.OLD_get_accessor_name() == name] + else: + matches = [f for f in field_list if f.name == name] if len(matches) != 1: return None return matches[0] @@ -293,7 +297,7 @@ def lookup_inner(path, clause, value, opts, table, column): raise FieldFound # Does the name belong to a one-to-many field? - field = find_field(name, opts.get_all_related_objects()) + field = find_field(name, current_opts.get_all_related_objects(), True) if field: new_table = table + LOOKUP_SEPARATOR + name new_opts = field.opts diff --git a/tests/modeltests/mutually_referential/models.py b/tests/modeltests/mutually_referential/models.py index 558d71cfa7..c4e1cb3def 100644 --- a/tests/modeltests/mutually_referential/models.py +++ b/tests/modeltests/mutually_referential/models.py @@ -8,7 +8,7 @@ from django.db.models import * class Parent(Model): name = CharField(maxlength=100) - bestchild = ForeignKey("Child", null=True) + bestchild = ForeignKey("Child", null=True, related_name="favoured_by") class Child(Model): name = CharField(maxlength=100)