Ticket #8913: unique-error-message-with-docs.diff
File unique-error-message-with-docs.diff, 4.6 KB (added by , 13 years ago) |
---|
-
docs/ref/models/fields.txt
210 210 field will raise. Pass in a dictionary with keys matching the error messages you 211 211 want to override. 212 212 213 Error message keys include ``null``, ``blank``, ``invalid``, ``invalid_choice``, 214 and ``unique``. Additional error message keys are specified for each field in 215 the `Field types`_ section below. 216 213 217 ``help_text`` 214 218 ------------- 215 219 … … 416 420 it's not just a default value that you can override. 417 421 418 422 The admin represents this as an ``<input type="text">`` with a JavaScript 419 calendar, and a shortcut for "Today". 423 calendar, and a shortcut for "Today". Includes an additional ``invalid_date`` 424 error message key. 420 425 421 426 .. note:: 422 427 As currently implemented, setting ``auto_now`` or ``auto_now_add`` to -
tests/modeltests/validation/models.py
78 78 slug = models.CharField(max_length=50, unique_for_year='posted', blank=True) 79 79 subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True) 80 80 posted = models.DateField(blank=True, null=True) 81 82 class UniqueErrorsModel(models.Model): 83 name = models.CharField(max_length=100, unique=True, error_messages={'unique': u'Custom unique name message.'}) 84 number = models.IntegerField(unique=True, error_messages={'unique': u'Custom unique number message.'}) 85 No newline at end of file -
tests/modeltests/validation/test_unique.py
7 7 from django.utils import unittest 8 8 9 9 from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, 10 UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost )10 UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, UniqueErrorsModel) 11 11 12 12 13 13 class GetUniqueCheckTests(unittest.TestCase): … … 149 149 self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.") 150 150 except: 151 151 self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.") 152 153 def test_unique_errors(self): 154 m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10) 155 m = UniqueErrorsModel(name='Some Name', number=11) 156 try: 157 m.full_clean() 158 except ValidationError, e: 159 self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']}) 160 except: 161 self.fail('unique checks should catch this.') 162 163 m = UniqueErrorsModel(name='Some Other Name', number=10) 164 try: 165 m.full_clean() 166 except ValidationError, e: 167 self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']}) 168 except: 169 self.fail('unique checks should catch this.') 170 171 No newline at end of file -
django/db/models/base.py
782 782 # A unique field 783 783 if len(unique_check) == 1: 784 784 field_name = unique_check[0] 785 field_label = capfirst(opts.get_field(field_name).verbose_name) 785 field = opts.get_field(field_name) 786 field_label = capfirst(field.verbose_name) 786 787 # Insert the error into the error dict, very sneaky 787 return _(u"%(model_name)s with this %(field_label)s already exists.")% {788 return field.error_messages['unique'] % { 788 789 'model_name': unicode(model_name), 789 790 'field_label': unicode(field_label) 790 791 } -
django/db/models/fields/__init__.py
60 60 'invalid_choice': _(u'Value %r is not a valid choice.'), 61 61 'null': _(u'This field cannot be null.'), 62 62 'blank': _(u'This field cannot be blank.'), 63 'unique': _(u'%(model_name)s with this %(field_label)s already exists.'), 63 64 } 64 65 65 66 # Generic field type description, usually overriden by subclasses