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
This commit is contained in:
Russell Keith-Magee 2006-01-28 17:06:05 +00:00
parent 90a49140a5
commit ce71979e53
2 changed files with 8 additions and 4 deletions

View File

@ -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

View File

@ -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)