Author: lukeplant
Date: 2006-04-28 12:59:37 -0500 (Fri, 28 Apr 2006)
New Revision: 2767

Modified:
   django/branches/magic-removal/django/db/models/base.py
Log:
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!)


Modified: django/branches/magic-removal/django/db/models/base.py
===================================================================
--- django/branches/magic-removal/django/db/models/base.py      2006-04-28 
05:05:56 UTC (rev 2766)
+++ django/branches/magic-removal/django/db/models/base.py      2006-04-28 
17:59:37 UTC (rev 2767)
@@ -79,34 +79,33 @@
 
     def __init__(self, *args, **kwargs):
         dispatcher.send(signal=signals.pre_init, sender=self.__class__, 
args=args, kwargs=kwargs)
-        if kwargs:
-            for f in self._meta.fields:
-                if isinstance(f.rel, ManyToOneRel):
+        for f in self._meta.fields:
+            if isinstance(f.rel, ManyToOneRel):
+                try:
+                    # Assume object instance was passed in.
+                    rel_obj = kwargs.pop(f.name)
+                except KeyError:
                     try:
-                        # Assume object instance was passed in.
-                        rel_obj = kwargs.pop(f.name)
+                        # Object instance wasn't passed in -- must be an ID.
+                        val = kwargs.pop(f.attname)
                     except KeyError:
-                        try:
-                            # 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)
+                        val = f.get_default()
                 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]
+                    # 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:
+                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):
             setattr(self, self._meta.fields[i].attname, arg)
         dispatcher.send(signal=signals.post_init, sender=self.__class__, 
instance=self)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to