Ticket #16986: model-clean-errordict-3.diff
File model-clean-errordict-3.diff, 5.3 KB (added by , 13 years ago) |
---|
-
tests/modeltests/validation/tests.py
8 8 9 9 from . import ValidationTestCase 10 10 from .models import (Author, Article, ModelToValidate, 11 GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest )11 GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest, Range) 12 12 # Import other tests for this package. 13 13 from .test_custom_messages import CustomMessagesTest 14 14 from .test_error_messages import ValidationMessagesTest … … 190 190 giptm.save() 191 191 giptm = GenericIPAddrUnpackUniqueTest(generic_v4unpack_ip="18.52.18.52") 192 192 self.assertFailsValidation(giptm.full_clean, ['generic_v4unpack_ip',]) 193 194 class RangeForm(forms.ModelForm): 195 class Meta: 196 model = Range 197 198 class ModelCleanPerFieldValidationTests(ValidationTestCase): 199 200 def test_no_errors(self): 201 range = Range(min=0, max=1) 202 range.full_clean() 203 204 form = RangeForm(instance=range) 205 form.full_clean() 206 self.assertEqual({}, form.errors) 207 208 def test_two_errors(self): 209 range = Range(min=999, max=1) 210 self.assertFieldFailsValidationWithMessage(range.full_clean, 'min', [u'Min value must be less that or equal to the max value.']) 211 self.assertFieldFailsValidationWithMessage(range.full_clean, 'max', [u'Max value must be greater than or equal to the min value.']) 212 213 # In particular, the key should not be '__all__' 214 expected_error_dict = { 215 'min': [u'Min value must be less that or equal to the max value.'], 216 'max': [u'Max value must be greater than or equal to the min value.'], 217 } 218 form = RangeForm(data={'min': 999, 'max': 1}) 219 form.full_clean() 220 self.assertEqual(expected_error_dict, form.errors) -
tests/modeltests/validation/models.py
1 from collections import defaultdict 1 2 from datetime import datetime 2 3 3 4 from django.core.exceptions import ValidationError … … 92 93 class GenericIPAddrUnpackUniqueTest(models.Model): 93 94 generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True) 94 95 96 class Range(models.Model): 97 min = models.IntegerField() 98 max = models.IntegerField() 99 100 def clean(self): 101 message_dict = defaultdict(list) 102 if self.min and self.max and (not self.min <= self.max): 103 message_dict['min'].append(u'Min value must be less that or equal to the max value.') 104 message_dict['max'].append(u'Max value must be greater than or equal to the min value.') 105 if len(message_dict): 106 raise ValidationError(message_dict) 95 107 96 108 # A model can't have multiple AutoFields 97 109 # Refs #12467. … … 102 114 auto2 = models.AutoField(primary_key=True) 103 115 except AssertionError, assertion_error: 104 116 pass # Fail silently 105 assert str(assertion_error) == u"A model can't have more than one AutoField." 106 No newline at end of file 117 assert str(assertion_error) == u"A model can't have more than one AutoField." -
django/forms/models.py
331 331 try: 332 332 self.instance.clean() 333 333 except ValidationError, e: 334 self._update_errors({NON_FIELD_ERRORS: e.messages}) 334 if e.has_message_dict: 335 self._update_errors(e.message_dict) 336 else: 337 self._update_errors({NON_FIELD_ERRORS: e.messages}) 335 338 336 339 # Validate uniqueness if needed. 337 340 if self._validate_unique: -
django/core/exceptions.py
59 59 self.params = params 60 60 message = force_unicode(message) 61 61 self.messages = [message] 62 62 63 @property 64 def has_message_dict(self): 65 return hasattr(self, 'message_dict') 66 63 67 def __str__(self): 64 68 # This is needed because, without a __str__(), printing an exception 65 69 # instance would result in this: 66 70 # AttributeError: ValidationError instance has no attribute 'args' 67 71 # See http://www.python.org/doc/current/tut/node10.html#handling 68 if hasattr(self, 'message_dict'):72 if self.has_message_dict: 69 73 return repr(self.message_dict) 70 74 return repr(self.messages) 71 75 72 76 def __repr__(self): 73 if hasattr(self, 'message_dict'):77 if self.has_message_dict: 74 78 return 'ValidationError(%s)' % repr(self.message_dict) 75 79 return 'ValidationError(%s)' % repr(self.messages) 76 80 77 81 def update_error_dict(self, error_dict): 78 if hasattr(self, 'message_dict'):82 if self.has_message_dict: 79 83 if error_dict: 80 84 for k, v in self.message_dict.items(): 81 85 error_dict.setdefault(k, []).extend(v)