1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

newforms: Changed model auto-Form generation so that create() and apply_changes() are now both called save() -- for the purposes of simplicity

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4299 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2007-01-09 05:22:48 +00:00
parent fb60a6ff0a
commit d08112a6a7
2 changed files with 28 additions and 27 deletions

View File

@@ -7,7 +7,7 @@ from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
__all__ = ('form_for_model', 'form_for_instance', 'form_for_fields')
def create(self, save=True):
def model_save(self, commit=True):
"""
Creates and returns model instance according to self.clean_data.
@@ -16,14 +16,14 @@ def create(self, save=True):
if self.errors:
raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
obj = self._model(**self.clean_data)
if save:
if commit:
obj.save()
return obj
def make_apply_changes(opts, instance):
"Returns the apply_changes() method for a form_for_instance Form."
def make_instance_save(opts, instance):
"Returns the save() method for a form_for_instance Form."
from django.db import models
def apply_changes(self, save=True):
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
@@ -31,7 +31,7 @@ def make_apply_changes(opts, instance):
if isinstance(f, models.AutoField):
continue
setattr(instance, f.attname, clean_data[f.name])
if save:
if commit:
instance.save()
return instance
return apply_changes
@@ -49,7 +49,7 @@ def form_for_model(model, form=BaseForm):
if formfield:
field_list.append((f.name, formfield))
fields = SortedDictFromList(field_list)
return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'create': create})
return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save})
def form_for_instance(instance, form=BaseForm):
"""
@@ -67,7 +67,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, 'apply_changes': make_apply_changes(opts, instance)})
{'fields': fields, '_model': model, 'save': make_instance_save(opts, instance)})
def form_for_fields(field_list):
"Returns a Form class for the given list of Django database field instances."