diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index e0a3b13111..ce9be1326e 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -332,7 +332,7 @@ def change_stage(request, app_label, model_name, object_id): wrt = related.opts.order_with_respect_to if wrt and wrt.rel and wrt.rel.to == opts: func = getattr(manipulator.original_object, 'get_%s_list' % - related.get_method_name_part()) + related.get_accessor_name()) orig_list = func() form.order_objects.extend(orig_list) @@ -362,7 +362,7 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current if related.opts in opts_seen: continue opts_seen.append(related.opts) - rel_opts_name = related.get_method_name_part() + rel_opts_name = related.get_accessor_name() if isinstance(related.field.rel, models.OneToOne): try: sub_obj = getattr(obj, 'get_%s' % rel_opts_name)() @@ -408,7 +408,7 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current if related.opts in opts_seen: continue opts_seen.append(related.opts) - rel_opts_name = related.get_method_name_part() + rel_opts_name = related.get_accessor_name() has_related_objs = False for sub_obj in getattr(obj, 'get_%s_list' % rel_opts_name)(): has_related_objs = True diff --git a/django/db/models/base.py b/django/db/models/base.py index bffd504f53..80c9ba0045 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -202,7 +202,7 @@ class Model(object): seen_objs.setdefault(self.__class__, {})[pk_val] = self for related in self._meta.get_all_related_objects(): - rel_opts_name = related.get_method_name_part() + rel_opts_name = related.get_accessor_name() if isinstance(related.field.rel, OneToOne): try: sub_obj = getattr(self, 'get_%s' % rel_opts_name)() diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 0035fbde2a..8b10e366e0 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -153,7 +153,7 @@ class ForeignKey(SharedMethods, Field): return Field.flatten_data(self, follow, obj) def contribute_to_related_class(self, cls, related): - rel_obj_name = related.get_method_name_part() + rel_obj_name = related.get_accessor_name() # Add "get_thingie" methods for many-to-one related objects. # EXAMPLE: Poll.get_choice() setattr(cls, 'get_%s' % rel_obj_name, curry(cls._get_related, method_name='get_object', rel_class=related.model, rel_field=related.field)) @@ -196,7 +196,7 @@ class OneToOneField(SharedMethods, IntegerField): self.deprecated_args.append(name) def contribute_to_related_class(self, cls, related): - rel_obj_name = related.get_method_name_part() + rel_obj_name = related.get_accessor_name() # Add "get_thingie" methods for one-to-one related objects. # EXAMPLE: Place.get_restaurants_restaurant() setattr(cls, 'get_%s' % rel_obj_name, @@ -286,7 +286,7 @@ class ManyToManyField(RelatedField, Field): setattr(cls, 'set_%s' % self.name, curry(cls._set_many_to_many_objects, field_with_rel=self)) def contribute_to_related_class(self, cls, related): - rel_obj_name = related.get_method_name_part() + rel_obj_name = related.get_accessor_name() setattr(cls, 'get_%s' % rel_obj_name, curry(cls._get_related_many_to_many, method_name='get_object', rel_class=related.model, rel_field=related.field)) setattr(cls, 'get_%s_count' % rel_obj_name, curry(cls._get_related_many_to_many, method_name='get_count', rel_class=related.model, rel_field=related.field)) setattr(cls, 'get_%s_list' % rel_obj_name, curry(cls._get_related_many_to_many, method_name='get_list', rel_class=related.model, rel_field=related.field)) diff --git a/django/db/models/manipulators.py b/django/db/models/manipulators.py index 8e45741b91..94f9265e65 100644 --- a/django/db/models/manipulators.py +++ b/django/db/models/manipulators.py @@ -154,7 +154,7 @@ class AutomaticManipulator(forms.Manipulator): if self.change: if rel_new_data[related.opts.pk.name][0]: try: - old_rel_obj = getattr(self.original_object, 'get_%s' % related.get_method_name_part() )(**{'%s__exact' % related.opts.pk.name: rel_new_data[related.opts.pk.attname][0]}) + old_rel_obj = getattr(self.original_object, 'get_%s' % related.get_accessor_name() )(**{'%s__exact' % related.opts.pk.name: rel_new_data[related.opts.pk.attname][0]}) except ObjectDoesNotExist: pass diff --git a/django/db/models/related.py b/django/db/models/related.py index 6def8d41b4..57a1ffad9d 100644 --- a/django/db/models/related.py +++ b/django/db/models/related.py @@ -44,7 +44,7 @@ class RelatedObject(object): def get_list(self, parent_instance=None): "Get the list of this type of object from an instance of the parent class." if parent_instance != None: - func_name = 'get_%s_list' % self.get_method_name_part() + func_name = 'get_%s_list' % self.get_accessor_name() func = getattr(parent_instance, func_name) return func() else: @@ -77,21 +77,22 @@ class RelatedObject(object): def bind(self, field_mapping, original, bound_related_object_class=BoundRelatedObject): return bound_related_object_class(self, field_mapping, original) - def get_method_name_part(self): - # This method encapsulates the logic that decides what name to give a - # method that retrieves related many-to-one or many-to-many objects. - # Usually it just uses the lower-cased object_name, but if the related - # object is in another app, the related object's app_label is appended. + def get_accessor_name(self): + # This method encapsulates the logic that decides what name to give an + # accessor descriptor that retrieves related many-to-one or + # many-to-many objects. Usually it just uses the lower-cased + # object_name, but if the related object is in another app, the related + # object's app_label is appended. # # Examples: # # # Normal case -- a related object in the same app. # # This method returns "choice". - # Poll.get_choice_list() + # Poll.choice_set # # # A related object in a different app. # # This method returns "lcom_bestofaward". - # Place.get_lcom_bestofaward_list() # "lcom_bestofaward" + # Place.lcom_bestofaward_set # "lcom_bestofaward" rel_obj_name = self.field.rel.related_name or self.opts.object_name.lower() if self.parent_opts.app_label != self.opts.app_label: rel_obj_name = '%s_%s' % (self.opts.app_label, rel_obj_name)