mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
magic-removal: Fixed #1705 - creating instances of models without supplying any keyword arguments.
(NB - diff is misleading - we need diff tools that support significant whitespace!) git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2767 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
84987b8a64
commit
d4c1b55cb2
@ -79,34 +79,33 @@ class Model(object):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
|
dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
|
||||||
if kwargs:
|
for f in self._meta.fields:
|
||||||
for f in self._meta.fields:
|
if isinstance(f.rel, ManyToOneRel):
|
||||||
if isinstance(f.rel, ManyToOneRel):
|
try:
|
||||||
|
# Assume object instance was passed in.
|
||||||
|
rel_obj = kwargs.pop(f.name)
|
||||||
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
# Assume object instance was passed in.
|
# Object instance wasn't passed in -- must be an ID.
|
||||||
rel_obj = kwargs.pop(f.name)
|
val = kwargs.pop(f.attname)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
val = f.get_default()
|
||||||
# Object instance wasn't passed in -- must be an ID.
|
|
||||||
val = kwargs.pop(f.attname)
|
|
||||||
except KeyError:
|
|
||||||
val = f.get_default()
|
|
||||||
else:
|
|
||||||
# Object instance was passed in.
|
|
||||||
# Special case: You can pass in "None" for related objects if it's allowed.
|
|
||||||
if rel_obj is None and f.null:
|
|
||||||
val = None
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
val = getattr(rel_obj, f.rel.get_related_field().attname)
|
|
||||||
except AttributeError:
|
|
||||||
raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj))
|
|
||||||
setattr(self, f.attname, val)
|
|
||||||
else:
|
else:
|
||||||
val = kwargs.pop(f.attname, f.get_default())
|
# Object instance was passed in.
|
||||||
setattr(self, f.attname, val)
|
# Special case: You can pass in "None" for related objects if it's allowed.
|
||||||
if kwargs:
|
if rel_obj is None and f.null:
|
||||||
raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
|
val = None
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
val = getattr(rel_obj, f.rel.get_related_field().attname)
|
||||||
|
except AttributeError:
|
||||||
|
raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj))
|
||||||
|
setattr(self, f.attname, val)
|
||||||
|
else:
|
||||||
|
val = kwargs.pop(f.attname, f.get_default())
|
||||||
|
setattr(self, f.attname, val)
|
||||||
|
if kwargs:
|
||||||
|
raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
|
||||||
for i, arg in enumerate(args):
|
for i, arg in enumerate(args):
|
||||||
setattr(self, self._meta.fields[i].attname, arg)
|
setattr(self, self._meta.fields[i].attname, arg)
|
||||||
dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
|
dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user