diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-03-06 18:42:56 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-03-06 18:42:56 +0000 |
| commit | b88f969789a5d69c770c25e1af9a2f4292184a6f (patch) | |
| tree | 0cd5e5e7772b14ed0c7172b58241c42df538b41b /django/forms/models.py | |
| parent | a2c4ad1dabf58130e0c97636dd401bb615f715ee (diff) | |
Fixed #12960. The return value of ModelForm.clean() is now applied to the model. Thanks for the report, krejcik.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/models.py')
| -rw-r--r-- | django/forms/models.py | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index 65fe1a7bd4..acfbe490d5 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -245,6 +245,10 @@ class BaseModelForm(BaseForm): # if initial was provided, it should override the values from instance if initial is not None: object_data.update(initial) + # self._validate_unique will be set to True by BaseModelForm.clean(). + # It is False by default so overriding self.clean() and failing to call + # super will stop validate_unique from being called. + self._validate_unique = False super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data, error_class, label_suffix, empty_permitted) @@ -299,34 +303,31 @@ class BaseModelForm(BaseForm): return exclude def clean(self): - self.validate_unique() + self._validate_unique = True return self.cleaned_data - def _clean_fields(self): - """ - Cleans the form fields, constructs the instance, then cleans the model - fields. - """ - super(BaseModelForm, self)._clean_fields() + def _post_clean(self): + exclude = self._get_validation_exclusions() opts = self._meta + + # Update the model instance with self.cleaned_data. self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude) - exclude = self._get_validation_exclusions() + + # Clean the model instance's fields. try: self.instance.clean_fields(exclude=exclude) except ValidationError, e: self._update_errors(e.message_dict) - def _clean_form(self): - """ - Runs the instance's clean method, then the form's. This is becuase the - form will run validate_unique() by default, and we should run the - model's clean method first. - """ + # Call the model instance's clean method. try: self.instance.clean() except ValidationError, e: self._update_errors({NON_FIELD_ERRORS: e.messages}) - super(BaseModelForm, self)._clean_form() + + # Validate uniqueness if needed. + if self._validate_unique: + self.validate_unique() def validate_unique(self): """ |
