diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py index 38ee70b169..482979c59e 100644 --- a/django/contrib/auth/tests/forms.py +++ b/django/contrib/auth/tests/forms.py @@ -16,7 +16,7 @@ FORM_TESTS = """ >>> form.is_valid() False >>> form["username"].errors -[u'A user with that username already exists.', u'This field cannot be blank.'] +[u'A user with that username already exists.'] # The username contains invalid data. @@ -29,7 +29,7 @@ False >>> form.is_valid() False >>> form["username"].errors -[u'This value must contain only letters, numbers and underscores.', u'This field cannot be blank.'] +[u'This value must contain only letters, numbers and underscores.'] # The verification password is incorrect. diff --git a/django/db/models/base.py b/django/db/models/base.py index 32804c3622..8cccab340a 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -606,13 +606,15 @@ class Model(object): """ pass - def clean(self): + def clean(self, exclude=[]): """ Cleans all fields and raises ValidationError containing message_dict of all validation errors if any occur. """ errors = {} for f in self._meta.fields: + if f.name in exclude: + continue try: # TODO: is the [sg]etattr correct? setattr(self, f.attname, f.clean(getattr(self, f.attname), self)) diff --git a/django/forms/models.py b/django/forms/models.py index 40cde06bc1..192ab8e8f4 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -243,8 +243,7 @@ class BaseModelForm(BaseForm): self.instance = make_instance(self, self.instance, opts.fields, opts.exclude) self.validate_unique() try: - # FIMXE: what to do about duplicate errors? (is required etc.) - self.instance.clean() + self.instance.clean(exclude=self._errors.keys()) except ValidationError, e: for k, v in e.message_dict.items(): if k != NON_FIELD_ERRORS: diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index a8a414143e..89fd56efe9 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -449,9 +449,9 @@ u'third-test' If you call save() with invalid data, you'll get a ValueError. >>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'}) >>> f.errors['name'] -[u'This field is required.', u'This field cannot be blank.'] +[u'This field is required.'] >>> f.errors['slug'] -[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.", u'This field cannot be blank.'] +[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."] >>> f.cleaned_data Traceback (most recent call last): ...