Author: jkocherhans
Date: 2010-01-04 17:34:54 -0600 (Mon, 04 Jan 2010)
New Revision: 12090

Modified:
   django/branches/soc2009/model-validation/django/forms/models.py
Log:
[soc2009/model-validation] Combined save_made_instance and save_instnace for 
simplicity.

Modified: django/branches/soc2009/model-validation/django/forms/models.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/models.py     
2010-01-04 22:48:36 UTC (rev 12089)
+++ django/branches/soc2009/model-validation/django/forms/models.py     
2010-01-04 23:34:54 UTC (rev 12090)
@@ -29,7 +29,12 @@
     'ModelMultipleChoiceField',
 )
 
-def make_instance(form, instance, fields=None, exclude=None):
+def construct_instance(form, instance, fields=None, exclude=None):
+    """
+    Constructs and returns a model instance from the bound ``form``'s
+    ``cleaned_data``, but does not save the returned instance to the
+    database.
+    """
     from django.db import models
     opts = instance._meta
 
@@ -59,7 +64,19 @@
 
     return instance
 
-def save_made_instance(form, instance, fields=None, commit=True, 
fail_message='saved'):
+def save_instance(form, instance, fields=None, fail_message='saved',
+                  commit=True, exclude=None, construct=True):
+    """
+    Saves bound Form ``form``'s cleaned_data into model instance ``instance``.
+
+    If commit=True, then the changes to ``instance`` will be saved to the
+    database. Returns ``instance``.
+
+    If construct=False, assume ``instance`` has already been constructed and
+    just needs to be saved.
+    """
+    if construct:
+        instance = construct_instance(form, instance, fields, exclude)
     opts = instance._meta
     if form.errors:
         raise ValueError("The %s could not be %s because the data didn't"
@@ -83,18 +100,6 @@
         form.save_m2m = save_m2m
     return instance
 
-
-def save_instance(form, instance, fields=None, fail_message='saved',
-                  commit=True, exclude=None):
-    """
-    Saves bound Form ``form``'s cleaned_data into model instance ``instance``.
-
-    If commit=True, then the changes to ``instance`` will be saved to the
-    database. Returns ``instance``.
-    """
-    instance = make_instance(form, instance, fields, exclude)
-    return save_made_instance(form, instance, fields, commit, fail_message)
-
 def make_model_save(model, fields, fail_message):
     """Returns the save() method for a Form."""
     def save(self, commit=True):
@@ -242,7 +247,7 @@
 
     def clean(self):
         opts = self._meta
-        self.instance = make_instance(self, self.instance, opts.fields, 
opts.exclude)
+        self.instance = construct_instance(self, self.instance, opts.fields, 
opts.exclude)
         try:
             self.instance.full_validate(exclude=self._errors.keys())
         except ValidationError, e:
@@ -276,7 +281,8 @@
             fail_message = 'created'
         else:
             fail_message = 'changed'
-        return save_made_instance(self, self.instance, self._meta.fields, 
commit, fail_message)
+        return save_instance(self, self.instance, self._meta.fields,
+                             fail_message, commit, construct=False)
 
     save.alters_data = True
 

--

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?hl=en.


Reply via email to