diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-02-10 00:34:45 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2010-02-10 00:34:45 +0000 |
| commit | 8f4540b2e68d4b9143d38d8af197cb4b794821b4 (patch) | |
| tree | 70d65310d72d09460ef2ac3bd9a84abaee458f56 /tests/regressiontests/model_forms_regress | |
| parent | 4682d693c4ab5447619d72be5626c4bbdcb655cd (diff) | |
Fixed #12698. Model.clean() used with a ModelForm no longer causes a KeyError when raising a ValidationError.
Note that previously it was possible to raise a ValidationError in the same place with a message_dict attribute. That behavior was a bug and will no longer have the same behavior.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12402 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/model_forms_regress')
| -rw-r--r-- | tests/regressiontests/model_forms_regress/models.py | 10 | ||||
| -rw-r--r-- | tests/regressiontests/model_forms_regress/tests.py | 19 |
2 files changed, 26 insertions, 3 deletions
diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py index 376586e94f..23ac745274 100644 --- a/tests/regressiontests/model_forms_regress/models.py +++ b/tests/regressiontests/model_forms_regress/models.py @@ -1,5 +1,7 @@ import os from django.db import models +from django.core.exceptions import ValidationError + class Person(models.Model): name = models.CharField(max_length=100) @@ -37,3 +39,11 @@ class CustomFileField(models.FileField): class CustomFF(models.Model): f = CustomFileField(upload_to='unused', blank=True) + +class RealPerson(models.Model): + name = models.CharField(max_length=100) + + def clean(self): + if self.name.lower() == 'anonymous': + raise ValidationError("Please specify a real name.") + diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py index 57d56551e5..3eeecb28e4 100644 --- a/tests/regressiontests/model_forms_regress/tests.py +++ b/tests/regressiontests/model_forms_regress/tests.py @@ -6,7 +6,8 @@ from django.forms.models import modelform_factory from django.conf import settings from django.test import TestCase -from models import Person, Triple, FilePathModel, Article, Publication, CustomFF +from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF + class ModelMultipleChoiceFieldTests(TestCase): @@ -117,7 +118,7 @@ class CFFForm(forms.ModelForm): class CustomFieldSaveTests(TestCase): def test_save(self): "Regression for #11149: save_form_data should be called only once" - + # It's enough that the form saves without error -- the custom save routine will # generate an AssertionError if it is called more than once during save. form = CFFForm(data = {'f': None}) @@ -129,8 +130,20 @@ class ModelChoiceIteratorTests(TestCase): class Meta: model = Article fields = ["publications"] - + Publication.objects.create(title="Pravda", date_published=date(1991, 8, 22)) f = Form() self.assertEqual(len(f.fields["publications"].choices), 1) + +class RealPersonForm(forms.ModelForm): + class Meta: + model = RealPerson + +class CustomModelFormSaveMethod(TestCase): + def test_string_message(self): + data = {'name': 'anonymous'} + form = RealPersonForm(data) + self.assertEqual(form.is_valid(), False) + self.assertEqual(form.errors['__all__'], ['Please specify a real name.']) + |
