1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

magic-removal: Collapsed else statements in SingleRelatedObjectDescriptor and ManyRelatedObjectsDescriptor

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2242 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-02-03 19:11:04 +00:00
parent fe94847e2c
commit ce0c6887d6

View File

@ -74,27 +74,27 @@ class SingleRelatedObjectDescriptor(object):
def __get__(self, instance, instance_type=None): def __get__(self, instance, instance_type=None):
if instance is None: if instance is None:
raise AttributeError, "%s must be accessed via instance" % self._field.name raise AttributeError, "%s must be accessed via instance" % self._field.name
else: cache_name = self._field.get_cache_name()
cache_name = self._field.get_cache_name() try:
try: return getattr(instance, cache_name)
return getattr(instance, cache_name) except AttributeError:
except AttributeError: val = getattr(instance, self._field.attname)
val = getattr(instance, self._field.attname) if val is None:
if val is None: raise self._field.rel.to.DoesNotExist
raise self._field.rel.to.DoesNotExist other_field = self._field.rel.get_related_field()
other_field = self._field.rel.get_related_field() if other_field.rel:
if other_field.rel: params = {'%s__%s__exact' % (self._field.rel.field_name, other_field.rel.field_name): val}
params = {'%s__%s__exact' % (self._field.rel.field_name, other_field.rel.field_name): val} else:
else: params = {'%s__exact' % self._field.rel.field_name: val}
params = {'%s__exact' % self._field.rel.field_name: val} rel_obj = self._field.rel.to._default_manager.get(**params)
rel_obj = self._field.rel.to._default_manager.get(**params) setattr(instance, cache_name, rel_obj)
setattr(instance, cache_name, rel_obj) return rel_obj
return rel_obj
class ManyRelatedObjectsDescriptor(object): class ManyRelatedObjectsDescriptor(object):
# This class provides the functionality that makes the related-object # This class provides the functionality that makes the related-object
# managers available as attributes on a model class, for fields that have # managers available as attributes on a model class, for fields that have
# multiple "remote" values. # multiple "remote" values and have a ManyToManyField pointed at them by
# some other model (rather than having a ManyToManyField themselves).
# In the example "poll.choice_set", the choice_set attribute is a # In the example "poll.choice_set", the choice_set attribute is a
# ManyRelatedObjectsDescriptor instance. # ManyRelatedObjectsDescriptor instance.
def __init__(self, related, rel_type): def __init__(self, related, rel_type):
@ -104,36 +104,35 @@ class ManyRelatedObjectsDescriptor(object):
def __get__(self, instance, instance_type=None): def __get__(self, instance, instance_type=None):
if instance is None: if instance is None:
raise AttributeError, "Manager must be accessed via instance" raise AttributeError, "Manager must be accessed via instance"
# Dynamically create a class that subclasses the related
# model's default manager.
superclass = self.related.model._default_manager.__class__
class_ = types.ClassType('RelatedManager', (superclass,), {})
# Override get_query_set on the RelatedManager
def get_query_set(self):
return superclass.get_query_set(self).filter(**(self.core_filters))
class_.get_query_set = get_query_set
manager = class_()
# Set core_filters on the new manager to limit it to the
# foreign-key relationship.
rel_field = self.related.field
if self.rel_type == 'o2m':
manager.core_filters = {'%s__%s__exact' % (rel_field.name, rel_field.rel.to._meta.pk.name): getattr(instance, rel_field.rel.get_related_field().attname)}
else: else:
# Dynamically create a class that subclasses the related manager.core_filters = {'%s__%s__exact' % (rel_field.name, instance_type._meta.pk.name): instance._get_pk_val()}
# model's default manager. manager.core_values = {rel_field.name: instance}
superclass = self.related.model._default_manager.__class__
class_ = types.ClassType('RelatedManager', (superclass,), {})
# Override get_query_set on the RelatedManager
def get_query_set(self):
return superclass.get_query_set(self).filter(**(self.core_filters))
class_.get_query_set = get_query_set
manager = class_()
# Set core_filters on the new manager to limit it to the # Prepare the manager.
# foreign-key relationship. # TODO: Fix this hack?
rel_field = self.related.field # We're setting self.manager.model here because
# self.manager._prepare() expects that self.manager.model is
# set. This is slightly hackish.
manager.model = self.related.model
manager._prepare()
if self.rel_type == 'o2m': return manager
manager.core_filters = {'%s__%s__exact' % (rel_field.name, rel_field.rel.to._meta.pk.name): getattr(instance, rel_field.rel.get_related_field().attname)}
else:
manager.core_filters = {'%s__%s__exact' % (rel_field.name, instance_type._meta.pk.name): instance._get_pk_val()}
manager.core_values = {rel_field.name: instance}
# Prepare the manager.
# TODO: Fix this hack?
# We're setting self.manager.model here because
# self.manager._prepare() expects that self.manager.model is
# set. This is slightly hackish.
manager.model = self.related.model
manager._prepare()
return manager
class ForeignKey(RelatedField, Field): class ForeignKey(RelatedField, Field):
empty_strings_allowed = False empty_strings_allowed = False