Browse files

[soc2009/model-validation] Combined save_made_instance and save_instn…

…ace for simplicity.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@12090 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent a54ed5a commit 82c4f9d04e9f0f8776037b6caf1343074518d303 @jkocherhans jkocherhans committed Jan 4, 2010
Showing with 22 additions and 16 deletions.
  1. +22 −16 django/forms/models.py
View
38 django/forms/models.py
@@ -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 @@ def make_instance(form, instance, fields=None, exclude=None):
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 @@ def save_m2m():
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 __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
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 @@ def save(self, commit=True):
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

0 comments on commit 82c4f9d

Please sign in to comment.