magic-removal: Refactored accessor to name used to build related queries.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2362 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2006-02-19 08:35:40 +00:00
parent 96649cc329
commit b730754415
2 changed files with 11 additions and 6 deletions

View File

@ -607,13 +607,13 @@ class FieldFound(Exception):
"Exception used to short circuit field-finding operations."
pass
def find_field(name, field_list, use_accessor=False):
def find_field(name, field_list, related_query):
"""
Finds a field with a specific name in a list of field instances.
Returns None if there are no matches, or several matches.
"""
if use_accessor:
matches = [f for f in field_list if (f.field.rel.related_name or f.opts.object_name.lower()) == name]
if related_query:
matches = [f for f in field_list if f.get_query_name() == name]
else:
matches = [f for f in field_list if f.name == name]
if len(matches) != 1:
@ -637,7 +637,7 @@ def lookup_inner(path, clause, value, opts, table, column):
# Try to find the name in the fields associated with the current class
try:
# Does the name belong to a defined many-to-many field?
field = find_field(name, current_opts.many_to_many)
field = find_field(name, current_opts.many_to_many, False)
if field:
new_table = current_table + LOOKUP_SEPARATOR + name
new_opts = field.rel.to._meta
@ -654,7 +654,7 @@ def lookup_inner(path, clause, value, opts, table, column):
raise FieldFound
# Does the name belong to a reverse defined many-to-many field?
field = find_field(name, current_opts.get_all_related_many_to_many_objects())
field = find_field(name, current_opts.get_all_related_many_to_many_objects(), True)
if field:
new_table = current_table + LOOKUP_SEPARATOR + name
new_opts = field.opts
@ -684,7 +684,7 @@ def lookup_inner(path, clause, value, opts, table, column):
raise FieldFound
# Does the name belong to a one-to-one, many-to-one, or regular field?
field = find_field(name, current_opts.fields)
field = find_field(name, current_opts.fields, False)
if field:
if field.rel: # One-to-One/Many-to-one field
new_table = current_table + LOOKUP_SEPARATOR + name

View File

@ -75,3 +75,8 @@ class RelatedObject(object):
# but this can be overridden with the "related_name" option.
return self.field.rel.related_name or (self.opts.object_name.lower() + '_set')
def get_query_name(self):
# This method defines the name that can be used to identify this related object
# in a table-spanning query. It uses the lower-cased object_name by default,
# but this can be overridden with the "related_name" option.
return self.field.rel.related_name or self.opts.object_name.lower()