magic-removal: Removed support for old-style related object lookup (get_foo() methods)

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2158 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-30 00:46:47 +00:00
parent 2be3eefdff
commit 06842af5ab
2 changed files with 2 additions and 85 deletions

View File

@ -357,21 +357,6 @@ class Model(object):
setattr(self, cachename, get_image_dimensions(filename)) setattr(self, cachename, get_image_dimensions(filename))
return getattr(self, cachename) return getattr(self, cachename)
def _get_foreign_key_object(self, field_with_rel):
cache_var = field_with_rel.get_cache_name()
if not hasattr(self, cache_var):
val = getattr(self, field_with_rel.attname)
if val is None:
raise field_with_rel.rel.to.DoesNotExist
other_field = field_with_rel.rel.get_related_field()
if other_field.rel:
params = {'%s__%s__exact' % (field_with_rel.rel.field_name, other_field.rel.field_name): val}
else:
params = {'%s__exact' % field_with_rel.rel.field_name: val}
retrieved_obj = field_with_rel.rel.to._default_manager.get_object(**params)
setattr(self, cache_var, retrieved_obj)
return getattr(self, cache_var)
def _get_many_to_many_objects(self, field_with_rel): def _get_many_to_many_objects(self, field_with_rel):
cache_var = '_%s_cache' % field_with_rel.name cache_var = '_%s_cache' % field_with_rel.name
if not hasattr(self, cache_var): if not hasattr(self, cache_var):
@ -425,24 +410,6 @@ class Model(object):
_set_many_to_many_objects.alters_data = True _set_many_to_many_objects.alters_data = True
def _get_related(self, method_name, rel_class, rel_field, **kwargs):
kwargs['%s__%s__exact' % (rel_field.name, rel_field.rel.to._meta.pk.name)] = getattr(self, rel_field.rel.get_related_field().attname)
kwargs.update(rel_field.rel.lookup_overrides)
return getattr(rel_class._default_manager, method_name)(**kwargs)
def _add_related(self, rel_class, rel_field, *args, **kwargs):
init_kwargs = dict(zip([f.attname for f in rel_class._meta.fields if f != rel_field and not isinstance(f, AutoField)], args))
init_kwargs.update(kwargs)
for f in rel_class._meta.fields:
if isinstance(f, AutoField):
init_kwargs[f.attname] = None
init_kwargs[rel_field.name] = self
obj = rel_class(**init_kwargs)
obj.save()
return obj
_add_related.alters_data = True
# Handles related many-to-many object retrieval. # Handles related many-to-many object retrieval.
# Examples: Album.get_song(), Album.get_song_list(), Album.get_song_count() # Examples: Album.get_song(), Album.get_song_list(), Album.get_song_count()
def _get_related_many_to_many(self, method_name, rel_class, rel_field, **kwargs): def _get_related_many_to_many(self, method_name, rel_class, rel_field, **kwargs):

View File

@ -214,40 +214,11 @@ class ForeignKey(RelatedField, Field):
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
super(ForeignKey, self).contribute_to_class(cls, name) super(ForeignKey, self).contribute_to_class(cls, name)
setattr(cls, self.name, SingleRelatedObjectDescriptor(self)) setattr(cls, self.name, SingleRelatedObjectDescriptor(self))
# TODO: Delete the rest of this function
# to remove support for old-style related lookup.
# Add methods for many-to-one related objects.
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
setattr(cls, 'get_%s' % self.name, curry(cls._get_foreign_key_object, field_with_rel=self))
def contribute_to_related_class(self, cls, related): def contribute_to_related_class(self, cls, related):
setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related, 'o2m')) setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related, 'o2m'))
# TODO: Delete the rest of this function and RelatedObject.OLD_get_accessor_name()
# to remove support for old-style related lookup.
rel_obj_name = related.OLD_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))
# Add "get_thingie_count" methods for many-to-one related objects.
# EXAMPLE: Poll.get_choice_count()
setattr(cls, 'get_%s_count' % rel_obj_name, curry(cls._get_related, method_name='get_count', rel_class=related.model, rel_field=related.field))
# Add "get_thingie_list" methods for many-to-one related objects.
# EXAMPLE: Poll.get_choice_list()
setattr(cls, 'get_%s_list' % rel_obj_name, curry(cls._get_related, method_name='get_list', rel_class=related.model, rel_field=related.field))
# Add "add_thingie" methods for many-to-one related objects,
# but only for related objects that are in the same app.
# EXAMPLE: Poll.add_choice()
if related.opts.app_label == cls._meta.app_label:
func = lambda self, *args, **kwargs: self._add_related(related.model, related.field, *args, **kwargs)
setattr(cls, 'add_%s' % rel_obj_name, func)
class OneToOneField(RelatedField, IntegerField): class OneToOneField(RelatedField, IntegerField):
def __init__(self, to, to_field=None, **kwargs): def __init__(self, to, to_field=None, **kwargs):
kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID') kwargs['verbose_name'] = kwargs.get('verbose_name', 'ID')
@ -281,17 +252,10 @@ class OneToOneField(RelatedField, IntegerField):
def contribute_to_class(self, cls, name): def contribute_to_class(self, cls, name):
super(OneToOneField, self).contribute_to_class(cls, name) super(OneToOneField, self).contribute_to_class(cls, name)
# Add methods for many-to-one related objects. setattr(cls, self.name, SingleRelatedObjectDescriptor(self))
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
setattr(cls, 'get_%s' % self.name, curry(cls._get_foreign_key_object, field_with_rel=self))
def contribute_to_related_class(self, cls, related): def contribute_to_related_class(self, cls, related):
rel_obj_name = related.OLD_get_accessor_name() setattr(cls, related.get_accessor_name(), SingleRelatedObjectDescriptor(self))
# Add "get_thingie" methods for one-to-one related objects.
# EXAMPLE: Place.get_restaurants_restaurant()
setattr(cls, 'get_%s' % rel_obj_name,
curry(cls._get_related, method_name='get_object',
rel_class=related.model, rel_field=related.field))
if not cls._meta.one_to_one_field: if not cls._meta.one_to_one_field:
cls._meta.one_to_one_field = self cls._meta.one_to_one_field = self
@ -377,20 +341,6 @@ class ManyToManyField(RelatedField, Field):
def contribute_to_related_class(self, cls, related): def contribute_to_related_class(self, cls, related):
setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related, 'm2m')) setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related, 'm2m'))
# TODO: Delete the rest of this function and RelatedObject.OLD_get_accessor_name()
# to remove support for old-style related lookup.
rel_obj_name = related.OLD_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))
if related.opts.app_label == cls._meta.app_label:
func = curry(cls._set_related_many_to_many, cls, related.field)
func.alters_data = True
setattr(cls, 'set_%s' % related.opts.module_name, func)
self.rel.singular = self.rel.singular or self.rel.to._meta.object_name.lower() self.rel.singular = self.rel.singular or self.rel.to._meta.object_name.lower()
def set_attributes_from_rel(self): def set_attributes_from_rel(self):