diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2007-05-15 03:37:41 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2007-05-15 03:37:41 +0000 |
| commit | 433659139596a75eab03940ea2029970de6ee287 (patch) | |
| tree | b4ee37f43df2d7a560a403a9d391c51702946772 /django/newforms | |
| parent | 415e84ad53e0d0d8f7df87784c1893489bdbe0b8 (diff) | |
newforms-admin: Merged to [5243]. There are 3 failing tests in regressiontests.serializers_regress.tests.SerializerTests, but they fail in trunk also.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/forms.py | 16 | ||||
| -rw-r--r-- | django/newforms/formsets.py | 20 | ||||
| -rw-r--r-- | django/newforms/models.py | 59 |
3 files changed, 51 insertions, 44 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 42a06fd00d..0eec71a516 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -187,13 +187,13 @@ class BaseForm(StrAndUnicode): def full_clean(self): """ - Cleans all of self.data and populates self.__errors and self.clean_data. + Cleans all of self.data and populates self.__errors and self.cleaned_data. """ errors = ErrorDict() if not self.is_bound: # Stop further processing. self.__errors = errors return - self.clean_data = {} + self.cleaned_data = {} for name, field in self.fields.items(): # value_from_datadict() gets the data from the dictionary. # Each widget type knows how to retrieve its own data, because some @@ -201,18 +201,18 @@ class BaseForm(StrAndUnicode): value = field.widget.value_from_datadict(self.data, self.add_prefix(name)) try: value = field.clean(value) - self.clean_data[name] = value + self.cleaned_data[name] = value if hasattr(self, 'clean_%s' % name): value = getattr(self, 'clean_%s' % name)() - self.clean_data[name] = value + self.cleaned_data[name] = value except ValidationError, e: errors[name] = e.messages try: - self.clean_data = self.clean() + self.cleaned_data = self.clean() except ValidationError, e: errors[NON_FIELD_ERRORS] = e.messages if errors: - delattr(self, 'clean_data') + delattr(self, 'cleaned_data') self.__errors = errors def clean(self): @@ -222,7 +222,7 @@ class BaseForm(StrAndUnicode): not be associated with a particular field; it will have a special-case association with the field named '__all__'. """ - return self.clean_data + return self.cleaned_data class Form(BaseForm): "A collection of Fields, plus their associated data." @@ -273,6 +273,8 @@ class BoundField(StrAndUnicode): attrs['id'] = auto_id if not self.form.is_bound: data = self.form.initial.get(self.name, self.field.initial) + if callable(data): + data = data() else: data = self.data return widget.render(self.html_name, data, attrs=attrs) diff --git a/django/newforms/formsets.py b/django/newforms/formsets.py index 330fb088b0..6eaaf12a5e 100644 --- a/django/newforms/formsets.py +++ b/django/newforms/formsets.py @@ -33,7 +33,7 @@ class BaseFormSet(object): if data: self.management_form = ManagementForm(data, auto_id=self.auto_id, prefix=self.prefix) if self.management_form.is_valid(): - self.total_forms = self.management_form.clean_data[FORM_COUNT_FIELD_NAME] + self.total_forms = self.management_form.cleaned_data[FORM_COUNT_FIELD_NAME] self.required_forms = self.total_forms - self.num_extra else: # not sure that ValidationError is the best thing to raise here @@ -67,7 +67,7 @@ class BaseFormSet(object): def full_clean(self): """ - Cleans all of self.data and populates self.__errors and self.clean_data. + Cleans all of self.data and populates self.__errors and self.cleaned_data. """ is_valid = True @@ -75,7 +75,7 @@ class BaseFormSet(object): if not self.is_bound: # Stop further processing. self.__errors = errors return - clean_data = [] + cleaned_data = [] deleted_data = [] self._form_list = [] @@ -103,12 +103,12 @@ class BaseFormSet(object): self.add_fields(form, i) else: # if the formset is still vaild overall and this form instance - # is valid, keep appending to clean_data + # is valid, keep appending to cleaned_data if is_valid and form.is_valid(): - if self.deletable and form.clean_data[DELETION_FIELD_NAME]: - deleted_data.append(form.clean_data) + if self.deletable and form.cleaned_data[DELETION_FIELD_NAME]: + deleted_data.append(form.cleaned_data) else: - clean_data.append(form.clean_data) + cleaned_data.append(form.cleaned_data) else: is_valid = False # append to errors regardless @@ -117,14 +117,14 @@ class BaseFormSet(object): deleted_data.reverse() if self.orderable: - clean_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME]) + cleaned_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME]) else: - clean_data.reverse() + cleaned_data.reverse() errors.reverse() self._form_list.reverse() if is_valid: - self.clean_data = clean_data + self.cleaned_data = cleaned_data self.deleted_data = deleted_data self.errors = errors self._is_valid = is_valid diff --git a/django/newforms/models.py b/django/newforms/models.py index a60002b705..9d7d027031 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -12,19 +12,9 @@ from widgets import Select, SelectMultiple, MultipleHiddenInput __all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields', 'ModelChoiceField', 'ModelMultipleChoiceField') -def model_save(self, commit=True): +def save_instance(form, instance, fields=None, fail_message='saved', commit=True): """ - Creates and returns model instance according to self.clean_data. - - This method is created for any form_for_model Form. - """ - if self.errors: - raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) - return save_instance(self, self._model(), commit) - -def save_instance(form, instance, commit=True): - """ - Saves bound Form ``form``'s clean_data into model instance ``instance``. + Saves bound Form ``form``'s cleaned_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 @@ -33,30 +23,40 @@ def save_instance(form, instance, commit=True): from django.db import models 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 + raise ValueError("The %s could not be %s because the data didn't validate." % (opts.object_name, fail_message)) + cleaned_data = form.cleaned_data for f in opts.fields: - if not f.editable or isinstance(f, models.AutoField) or not f.name in clean_data: + if not f.editable or isinstance(f, models.AutoField) or not f.name in cleaned_data: + continue + if fields and f.name not in fields: continue - setattr(instance, f.name, clean_data[f.name]) + setattr(instance, f.name, cleaned_data[f.name]) if commit: instance.save() for f in opts.many_to_many: - if f.name in clean_data: - setattr(instance, f.attname, clean_data[f.name]) + if fields and f.name not in fields: + continue + if f.name in cleaned_data: + setattr(instance, f.attname, cleaned_data[f.name]) # GOTCHA: If many-to-many data is given and commit=False, the many-to-many # data will be lost. This happens because a many-to-many options cannot be # set on an object until after it's saved. Maybe we should raise an # exception in that case. return instance -def make_instance_save(instance): - "Returns the save() method for a form_for_instance Form." +def make_model_save(model, fields, fail_message): + "Returns the save() method for a Form." def save(self, commit=True): - return save_instance(self, instance, commit) + return save_instance(self, model(), fields, fail_message, commit) + return save + +def make_instance_save(instance, fields, fail_message): + "Returns the save() method for a Form." + def save(self, commit=True): + return save_instance(self, instance, fields, fail_message, commit) return save -def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield()): +def form_for_model(model, form=BaseForm, fields=None, formfield_callback=lambda f: f.formfield()): """ Returns a Form class for the given Django model class. @@ -71,13 +71,16 @@ def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfiel for f in opts.fields + opts.many_to_many: if not f.editable: continue + if fields and not f.name in fields: + continue formfield = formfield_callback(f) if formfield: field_list.append((f.name, formfield)) - fields = SortedDictFromList(field_list) - return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save}) + base_fields = SortedDictFromList(field_list) + return type(opts.object_name + 'Form', (form,), + {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')}) -def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): +def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): """ Returns a Form class for the given Django model instance. @@ -94,13 +97,15 @@ def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kw for f in opts.fields + opts.many_to_many: if not f.editable: continue + if fields and not f.name in fields: + continue current_value = f.value_from_object(instance) formfield = formfield_callback(f, initial=current_value) if formfield: field_list.append((f.name, formfield)) - fields = SortedDictFromList(field_list) + base_fields = SortedDictFromList(field_list) return type(opts.object_name + 'InstanceForm', (form,), - {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)}) + {'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance, fields, 'changed')}) def form_for_fields(field_list): "Returns a Form class for the given list of Django database field instances." |
