1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #3232 -- newforms: Added save_instance(), which saves a given bound form's clean_data into a given model instance with the same field names

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4300 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2007-01-09 05:49:47 +00:00
parent d08112a6a7
commit 78b89ff118
2 changed files with 51 additions and 22 deletions

View File

@@ -5,7 +5,7 @@ and database field objects.
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
__all__ = ('form_for_model', 'form_for_instance', 'form_for_fields')
__all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields')
def model_save(self, commit=True):
"""
@@ -20,27 +20,38 @@ def model_save(self, commit=True):
obj.save()
return obj
def make_instance_save(opts, instance):
"Returns the save() method for a form_for_instance Form."
def save_instance(form, instance, commit=True):
"""
Saves bound Form ``form``'s clean_data into model instance ``instance``.
Assumes ``form`` has a field for every non-AutoField database field in
``instance``. If commit=True, then the changes to ``instance`` will be
saved to the database. Returns ``instance``.
"""
from django.db import models
def apply_changes(self, commit=True):
if self.errors:
raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
clean_data = self.clean_data
for f in opts.fields + opts.many_to_many:
if isinstance(f, models.AutoField):
continue
setattr(instance, f.attname, clean_data[f.name])
if commit:
instance.save()
return instance
return apply_changes
opts = instance.__class__._meta
if form.errors:
raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
clean_data = form.clean_data
for f in opts.fields + opts.many_to_many:
if isinstance(f, models.AutoField):
continue
setattr(instance, f.attname, clean_data[f.name])
if commit:
instance.save()
return instance
def make_instance_save(instance):
"Returns the save() method for a form_for_instance Form."
def save(self, commit=True):
return save_instance(self, instance, commit)
return save
def form_for_model(model, form=BaseForm):
"""
Returns a Form class for the given Django model class.
Provide 'form' if you want to use a custom BaseForm subclass.
Provide ``form`` if you want to use a custom BaseForm subclass.
"""
opts = model._meta
field_list = []
@@ -55,7 +66,7 @@ def form_for_instance(instance, form=BaseForm):
"""
Returns a Form class for the given Django model instance.
Provide 'form' if you want to use a custom BaseForm subclass.
Provide ``form`` if you want to use a custom BaseForm subclass.
"""
model = instance.__class__
opts = model._meta
@@ -67,7 +78,7 @@ def form_for_instance(instance, form=BaseForm):
field_list.append((f.name, formfield))
fields = SortedDictFromList(field_list)
return type(opts.object_name + 'InstanceForm', (form,),
{'fields': fields, '_model': model, 'save': make_instance_save(opts, instance)})
{'fields': fields, '_model': model, 'save': make_instance_save(instance)})
def form_for_fields(field_list):
"Returns a Form class for the given list of Django database field instances."