magic-removal: Refactored ManyRelatedObjectsDescriptor and ReverseManyRelatedObjectsDescriptor to use normal class syntax in creation of RelatedManager, rather than using types module. It's more readable now.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2252 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
36c66d7d39
commit
e456bbb752
|
@ -7,7 +7,6 @@ from django.utils.functional import curry
|
|||
from django.core import validators
|
||||
from django import forms
|
||||
from django.dispatch import dispatcher
|
||||
import types
|
||||
|
||||
# Values for Relation.edit_inline.
|
||||
TABULAR, STACKED = 1, 2
|
||||
|
@ -105,25 +104,26 @@ class ManyRelatedObjectsDescriptor(object):
|
|||
def __get__(self, instance, instance_type=None):
|
||||
if instance is None:
|
||||
raise AttributeError, "Manager must be accessed via instance"
|
||||
|
||||
rel_field = self.related.field
|
||||
|
||||
# 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
|
||||
class RelatedManager(superclass):
|
||||
def get_query_set(self):
|
||||
return superclass.get_query_set(self).filter(**(self.core_filters))
|
||||
def add(self, **kwargs):
|
||||
kwargs.update({rel_field.name: instance})
|
||||
return superclass.add(self, **kwargs)
|
||||
|
||||
manager = RelatedManager()
|
||||
|
||||
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:
|
||||
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?
|
||||
|
@ -157,19 +157,19 @@ class ReverseManyRelatedObjectsDescriptor(object):
|
|||
# Dynamically create a class that subclasses the related
|
||||
# model's default manager.
|
||||
superclass = self.rel_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).extra(
|
||||
tables=(join_table,),
|
||||
where=[
|
||||
'%s.%s = %s.%s' % (qn(rel_opts.db_table), qn(rel_opts.pk.column), join_table, rel_opts.object_name.lower() + '_id'),
|
||||
'%s.%s = %%s' % (join_table, this_opts.object_name.lower() + '_id')
|
||||
],
|
||||
params = [instance._get_pk_val()]
|
||||
)
|
||||
class_.get_query_set = get_query_set
|
||||
manager = class_()
|
||||
|
||||
class RelatedManager(superclass):
|
||||
def get_query_set(self):
|
||||
return superclass.get_query_set(self).extra(
|
||||
tables=(join_table,),
|
||||
where=[
|
||||
'%s.%s = %s.%s' % (qn(rel_opts.db_table), qn(rel_opts.pk.column), join_table, rel_opts.object_name.lower() + '_id'),
|
||||
'%s.%s = %%s' % (join_table, this_opts.object_name.lower() + '_id')
|
||||
],
|
||||
params = [instance._get_pk_val()]
|
||||
)
|
||||
|
||||
manager = RelatedManager()
|
||||
|
||||
# Prepare the manager.
|
||||
# TODO: Fix this hack?
|
||||
|
|
|
@ -24,11 +24,6 @@ class Manager(object):
|
|||
# Tracks each time a Manager instance is created. Used to retain order.
|
||||
creation_counter = 0
|
||||
|
||||
# Dictionary of field_name -> field_value that will always be used in add().
|
||||
# For example, if this is {'name': 'adrian'}, each object created by add() will
|
||||
# have name='adrian'.
|
||||
core_values = {}
|
||||
|
||||
def __init__(self):
|
||||
super(Manager, self).__init__()
|
||||
# Increase the creation counter, and save our local copy.
|
||||
|
@ -108,7 +103,6 @@ class Manager(object):
|
|||
#################
|
||||
|
||||
def add(self, **kwargs):
|
||||
kwargs.update(self.core_values)
|
||||
new_obj = self.model(**kwargs)
|
||||
new_obj.save()
|
||||
return new_obj
|
||||
|
|
Loading…
Reference in New Issue