Ticket #8913: unique-error-message-with-docs-author-update.diff

File unique-error-message-with-docs-author-update.diff, 5.0 KB (added by leahculver, 4 years ago)

Updated my email address in AUTHORS

  • AUTHORS

     
    128128    Robert Coup
    129129    Pete Crosier <pete.crosier@gmail.com>
    130130    Matt Croydon <http://www.postneo.com/>
    131     Leah Culver <leah@pownce.com>
     131    Leah Culver <leah.culver@gmail.com>
    132132    flavio.curella@gmail.com
    133133    Jure Cuhalev <gandalf@owca.info>
    134134    John D'Agostino <john.dagostino@gmail.com>
  • docs/ref/models/fields.txt

     
    210210field will raise. Pass in a dictionary with keys matching the error messages you
    211211want to override.
    212212
     213Error message keys include ``null``, ``blank``, ``invalid``, ``invalid_choice``,
     214and ``unique``. Additional error message keys are specified for each field in
     215the `Field types`_ section below.
     216
    213217``help_text``
    214218-------------
    215219
     
    416420    it's not just a default value that you can override.
    417421
    418422The admin represents this as an ``<input type="text">`` with a JavaScript
    419 calendar, and a shortcut for "Today".
     423calendar, and a shortcut for "Today". Includes an additional ``invalid_date``
     424error message key.
    420425
    421426.. note::
    422427    As currently implemented, setting ``auto_now`` or ``auto_now_add`` to
  • tests/modeltests/validation/models.py

     
    7878    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
    7979    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
    8080    posted = models.DateField(blank=True, null=True)
     81
     82class 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

     
    77from django.utils import unittest
    88
    99from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
    10     UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
     10    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, UniqueErrorsModel)
    1111
    1212
    1313class GetUniqueCheckTests(unittest.TestCase):
     
    149149            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
    150150        except:
    151151            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

     
    782782        # A unique field
    783783        if len(unique_check) == 1:
    784784            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)
    786787            # 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'] %  {
    788789                'model_name': unicode(model_name),
    789790                'field_label': unicode(field_label)
    790791            }
  • django/db/models/fields/__init__.py

     
    6060        'invalid_choice': _(u'Value %r is not a valid choice.'),
    6161        'null': _(u'This field cannot be null.'),
    6262        'blank': _(u'This field cannot be blank.'),
     63        'unique': _(u'%(model_name)s with this %(field_label)s already exists.'),
    6364    }
    6465
    6566    # Generic field type description, usually overriden by subclasses
Back to Top