magic-removal: Implemented Manager.add()

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2182 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-30 05:54:12 +00:00
parent e49a09731b
commit 13cea53bb2
2 changed files with 13 additions and 0 deletions

View File

@ -117,6 +117,7 @@ class ManyRelatedObjectsDescriptor(object):
manager.core_filters.update({'%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.update({'%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?

View File

@ -24,6 +24,11 @@ class Manager(QuerySet):
# 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.
@ -48,6 +53,13 @@ class Manager(QuerySet):
kwargs['limit'] = 1
return self.get_object(*args, **kwargs)
def add(self, **kwargs):
kwargs.update(self.core_values)
new_obj = self.klass(**kwargs)
new_obj.save()
return new_obj
add.alters_data = True
class ManagerDescriptor(object):
# This class ensures managers aren't accessible via model instances.
# For example, Poll.objects works, but poll_obj.objects raises AttributeError.