From b7307544152e8fe2985baafc2a76ea5b325d2ba6 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sun, 19 Feb 2006 08:35:40 +0000 Subject: [PATCH] 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 --- django/db/models/query.py | 12 ++++++------ django/db/models/related.py | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 4b40495f5a..f589c00a28 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -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 diff --git a/django/db/models/related.py b/django/db/models/related.py index 895a2a8c6d..a092b486c1 100644 --- a/django/db/models/related.py +++ b/django/db/models/related.py @@ -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()