Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 8f4540b2e68d4b9143d38d8af197cb4b794821b4 1 parent 4682d69
Joseph Kocherhans authored February 10, 2010
2  django/forms/models.py
@@ -317,7 +317,7 @@ def _clean_form(self):
317 317
         try:
318 318
             self.instance.clean()
319 319
         except ValidationError, e:
320  
-            self._update_errors(e.message_dict)
  320
+            self._update_errors({NON_FIELD_ERRORS: e.messages})
321 321
         super(BaseModelForm, self)._clean_form()
322 322
 
323 323
     def validate_unique(self):
10  tests/regressiontests/model_forms_regress/models.py
... ...
@@ -1,5 +1,7 @@
1 1
 import os
2 2
 from django.db import models
  3
+from django.core.exceptions import ValidationError
  4
+
3 5
 
4 6
 class Person(models.Model):
5 7
     name = models.CharField(max_length=100)
@@ -37,3 +39,11 @@ def save_form_data(self, instance, data):
37 39
 
38 40
 class CustomFF(models.Model):
39 41
     f = CustomFileField(upload_to='unused', blank=True)
  42
+
  43
+class RealPerson(models.Model):
  44
+    name = models.CharField(max_length=100)
  45
+
  46
+    def clean(self):
  47
+        if self.name.lower() == 'anonymous':
  48
+            raise ValidationError("Please specify a real name.")
  49
+
19  tests/regressiontests/model_forms_regress/tests.py
@@ -6,7 +6,8 @@
6 6
 from django.conf import settings
7 7
 from django.test import TestCase
8 8
 
9  
-from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
  9
+from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF
  10
+
10 11
 
11 12
 class ModelMultipleChoiceFieldTests(TestCase):
12 13
 
@@ -117,7 +118,7 @@ class Meta:
117 118
 class CustomFieldSaveTests(TestCase):
118 119
     def test_save(self):
119 120
         "Regression for #11149: save_form_data should be called only once"
120  
-        
  121
+
121 122
         # It's enough that the form saves without error -- the custom save routine will
122 123
         # generate an AssertionError if it is called more than once during save.
123 124
         form = CFFForm(data = {'f': None})
@@ -129,8 +130,20 @@ class Form(forms.ModelForm):
129 130
             class Meta:
130 131
                 model = Article
131 132
                 fields = ["publications"]
132  
-        
  133
+
133 134
         Publication.objects.create(title="Pravda",
134 135
             date_published=date(1991, 8, 22))
135 136
         f = Form()
136 137
         self.assertEqual(len(f.fields["publications"].choices), 1)
  138
+
  139
+class RealPersonForm(forms.ModelForm):
  140
+    class Meta:
  141
+        model = RealPerson
  142
+
  143
+class CustomModelFormSaveMethod(TestCase):
  144
+    def test_string_message(self):
  145
+        data = {'name': 'anonymous'}
  146
+        form = RealPersonForm(data)
  147
+        self.assertEqual(form.is_valid(), False)
  148
+        self.assertEqual(form.errors['__all__'], ['Please specify a real name.']) 
  149
+

0 notes on commit 8f4540b

Please sign in to comment.
Something went wrong with that request. Please try again.