Fixed #8913 - Make "must be unique" error messages customisable. Thanks to Leah Culver.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16345 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7f6675a5fb
commit
865d684a8a
2
AUTHORS
2
AUTHORS
|
@ -128,7 +128,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Robert Coup
|
Robert Coup
|
||||||
Pete Crosier <pete.crosier@gmail.com>
|
Pete Crosier <pete.crosier@gmail.com>
|
||||||
Matt Croydon <http://www.postneo.com/>
|
Matt Croydon <http://www.postneo.com/>
|
||||||
Leah Culver <leah@pownce.com>
|
Leah Culver <leah.culver@gmail.com>
|
||||||
flavio.curella@gmail.com
|
flavio.curella@gmail.com
|
||||||
Jure Cuhalev <gandalf@owca.info>
|
Jure Cuhalev <gandalf@owca.info>
|
||||||
John D'Agostino <john.dagostino@gmail.com>
|
John D'Agostino <john.dagostino@gmail.com>
|
||||||
|
|
|
@ -782,9 +782,10 @@ class Model(object):
|
||||||
# A unique field
|
# A unique field
|
||||||
if len(unique_check) == 1:
|
if len(unique_check) == 1:
|
||||||
field_name = unique_check[0]
|
field_name = unique_check[0]
|
||||||
field_label = capfirst(opts.get_field(field_name).verbose_name)
|
field = opts.get_field(field_name)
|
||||||
|
field_label = capfirst(field.verbose_name)
|
||||||
# Insert the error into the error dict, very sneaky
|
# Insert the error into the error dict, very sneaky
|
||||||
return _(u"%(model_name)s with this %(field_label)s already exists.") % {
|
return field.error_messages['unique'] % {
|
||||||
'model_name': unicode(model_name),
|
'model_name': unicode(model_name),
|
||||||
'field_label': unicode(field_label)
|
'field_label': unicode(field_label)
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ class Field(object):
|
||||||
'invalid_choice': _(u'Value %r is not a valid choice.'),
|
'invalid_choice': _(u'Value %r is not a valid choice.'),
|
||||||
'null': _(u'This field cannot be null.'),
|
'null': _(u'This field cannot be null.'),
|
||||||
'blank': _(u'This field cannot be blank.'),
|
'blank': _(u'This field cannot be blank.'),
|
||||||
|
'unique': _(u'%(model_name)s with this %(field_label)s already exists.'),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Generic field type description, usually overriden by subclasses
|
# Generic field type description, usually overriden by subclasses
|
||||||
|
|
|
@ -210,6 +210,10 @@ The ``error_messages`` argument lets you override the default messages that the
|
||||||
field will raise. Pass in a dictionary with keys matching the error messages you
|
field will raise. Pass in a dictionary with keys matching the error messages you
|
||||||
want to override.
|
want to override.
|
||||||
|
|
||||||
|
Error message keys include ``null``, ``blank``, ``invalid``, ``invalid_choice``,
|
||||||
|
and ``unique``. Additional error message keys are specified for each field in
|
||||||
|
the `Field types`_ section below.
|
||||||
|
|
||||||
``help_text``
|
``help_text``
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
@ -416,7 +420,8 @@ optional arguments:
|
||||||
it's not just a default value that you can override.
|
it's not just a default value that you can override.
|
||||||
|
|
||||||
The admin represents this as an ``<input type="text">`` with a JavaScript
|
The admin represents this as an ``<input type="text">`` with a JavaScript
|
||||||
calendar, and a shortcut for "Today".
|
calendar, and a shortcut for "Today". Includes an additional ``invalid_date``
|
||||||
|
error message key.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
As currently implemented, setting ``auto_now`` or ``auto_now_add`` to
|
As currently implemented, setting ``auto_now`` or ``auto_now_add`` to
|
||||||
|
|
|
@ -78,3 +78,7 @@ class FlexibleDatePost(models.Model):
|
||||||
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
||||||
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
||||||
posted = models.DateField(blank=True, null=True)
|
posted = models.DateField(blank=True, null=True)
|
||||||
|
|
||||||
|
class UniqueErrorsModel(models.Model):
|
||||||
|
name = models.CharField(max_length=100, unique=True, error_messages={'unique': u'Custom unique name message.'})
|
||||||
|
number = models.IntegerField(unique=True, error_messages={'unique': u'Custom unique number message.'})
|
|
@ -7,7 +7,7 @@ from django.test import TestCase
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
|
|
||||||
from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
|
from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
|
||||||
UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
|
UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, UniqueErrorsModel)
|
||||||
|
|
||||||
|
|
||||||
class GetUniqueCheckTests(unittest.TestCase):
|
class GetUniqueCheckTests(unittest.TestCase):
|
||||||
|
@ -149,3 +149,22 @@ class PerformUniqueChecksTest(TestCase):
|
||||||
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
|
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
|
||||||
except:
|
except:
|
||||||
self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
|
self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
|
||||||
|
|
||||||
|
def test_unique_errors(self):
|
||||||
|
m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10)
|
||||||
|
m = UniqueErrorsModel(name='Some Name', number=11)
|
||||||
|
try:
|
||||||
|
m.full_clean()
|
||||||
|
except ValidationError, e:
|
||||||
|
self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']})
|
||||||
|
except:
|
||||||
|
self.fail('unique checks should catch this.')
|
||||||
|
|
||||||
|
m = UniqueErrorsModel(name='Some Other Name', number=10)
|
||||||
|
try:
|
||||||
|
m.full_clean()
|
||||||
|
except ValidationError, e:
|
||||||
|
self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']})
|
||||||
|
except:
|
||||||
|
self.fail('unique checks should catch this.')
|
||||||
|
|
Loading…
Reference in New Issue