Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #8913 - Make "must be unique" error messages customisable. Than…

…ks to Leah Culver.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16345 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 865d684a8af8e3ef7a11c43df861e1a69b0d8cd4 1 parent 7f6675a
@andrewgodwin andrewgodwin authored
View
2  AUTHORS
@@ -128,7 +128,7 @@ answer newbie questions, and generally made Django that much better:
Robert Coup
Pete Crosier <pete.crosier@gmail.com>
Matt Croydon <http://www.postneo.com/>
- Leah Culver <leah@pownce.com>
+ Leah Culver <leah.culver@gmail.com>
flavio.curella@gmail.com
Jure Cuhalev <gandalf@owca.info>
John D'Agostino <john.dagostino@gmail.com>
View
5 django/db/models/base.py
@@ -782,9 +782,10 @@ def unique_error_message(self, model_class, unique_check):
# 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)
}
View
1  django/db/models/fields/__init__.py
@@ -60,6 +60,7 @@ class Field(object):
'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
View
7 docs/ref/models/fields.txt
@@ -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
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 @@ optional arguments:
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
View
4 tests/modeltests/validation/models.py
@@ -78,3 +78,7 @@ class FlexibleDatePost(models.Model):
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.'})
View
21 tests/modeltests/validation/test_unique.py
@@ -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 @@ def test_unique_for_date_with_nullable_date(self):
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.')
+
Please sign in to comment.
Something went wrong with that request. Please try again.