From ce71979e533c5c46432e7ed8a2c2322a7c733ca4 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 28 Jan 2006 17:06:05 +0000 Subject: [PATCH] magic-removal: Modified query lookup to correctly handle non-plural RelatedObjects. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2145 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 10 +++++++--- tests/modeltests/mutually_referential/models.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) 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)