Author: andrewgodwin
Date: 2011-06-09 08:05:13 -0700 (Thu, 09 Jun 2011)
New Revision: 16345
Modified:
django/trunk/AUTHORS
django/trunk/django/db/models/base.py
django/trunk/django/db/models/fields/__init__.py
django/trunk/docs/ref/models/fields.txt
django/trunk/tests/modeltests/validation/models.py
django/trunk/tests/modeltests/validation/test_unique.py
Log:
Fixed #8913 - Make "must be unique" error messages customisable. Thanks to Leah
Culver.
Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS 2011-06-09 13:13:19 UTC (rev 16344)
+++ django/trunk/AUTHORS 2011-06-09 15:05:13 UTC (rev 16345)
@@ -128,7 +128,7 @@
Robert Coup
Pete Crosier <[email protected]>
Matt Croydon <http://www.postneo.com/>
- Leah Culver <[email protected]>
+ Leah Culver <[email protected]>
[email protected]
Jure Cuhalev <[email protected]>
John D'Agostino <[email protected]>
Modified: django/trunk/django/db/models/base.py
===================================================================
--- django/trunk/django/db/models/base.py 2011-06-09 13:13:19 UTC (rev
16344)
+++ django/trunk/django/db/models/base.py 2011-06-09 15:05:13 UTC (rev
16345)
@@ -782,9 +782,10 @@
# A unique field
if len(unique_check) == 1:
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
- return _(u"%(model_name)s with this %(field_label)s already
exists.") % {
+ return field.error_messages['unique'] % {
'model_name': unicode(model_name),
'field_label': unicode(field_label)
}
Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py 2011-06-09 13:13:19 UTC
(rev 16344)
+++ django/trunk/django/db/models/fields/__init__.py 2011-06-09 15:05:13 UTC
(rev 16345)
@@ -60,6 +60,7 @@
'invalid_choice': _(u'Value %r is not a valid choice.'),
'null': _(u'This field cannot be null.'),
'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
Modified: django/trunk/docs/ref/models/fields.txt
===================================================================
--- django/trunk/docs/ref/models/fields.txt 2011-06-09 13:13:19 UTC (rev
16344)
+++ django/trunk/docs/ref/models/fields.txt 2011-06-09 15:05:13 UTC (rev
16345)
@@ -210,6 +210,10 @@
field will raise. Pass in a dictionary with keys matching the error messages
you
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``
-------------
@@ -416,7 +420,8 @@
it's not just a default value that you can override.
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::
As currently implemented, setting ``auto_now`` or ``auto_now_add`` to
Modified: django/trunk/tests/modeltests/validation/models.py
===================================================================
--- django/trunk/tests/modeltests/validation/models.py 2011-06-09 13:13:19 UTC
(rev 16344)
+++ django/trunk/tests/modeltests/validation/models.py 2011-06-09 15:05:13 UTC
(rev 16345)
@@ -78,3 +78,7 @@
slug = models.CharField(max_length=50, unique_for_year='posted',
blank=True)
subtitle = models.CharField(max_length=50, unique_for_month='posted',
blank=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.'})
\ No newline at end of file
Modified: django/trunk/tests/modeltests/validation/test_unique.py
===================================================================
--- django/trunk/tests/modeltests/validation/test_unique.py 2011-06-09
13:13:19 UTC (rev 16344)
+++ django/trunk/tests/modeltests/validation/test_unique.py 2011-06-09
15:05:13 UTC (rev 16345)
@@ -7,7 +7,7 @@
from django.utils import unittest
from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
- UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
+ UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost,
UniqueErrorsModel)
class GetUniqueCheckTests(unittest.TestCase):
@@ -149,3 +149,22 @@
self.fail("unique_for_month checks shouldn't trigger when the
associated DateField is None.")
except:
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.')
+
\ No newline at end of file
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.