Code

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, 3 years ago)

Updated my email address in AUTHORS

Line 
1Index: AUTHORS
2===================================================================
3--- AUTHORS     (revision 16341)
4+++ AUTHORS     (working copy)
5@@ -128,7 +128,7 @@
6     Robert Coup
7     Pete Crosier <pete.crosier@gmail.com>
8     Matt Croydon <http://www.postneo.com/>
9-    Leah Culver <leah@pownce.com>
10+    Leah Culver <leah.culver@gmail.com>
11     flavio.curella@gmail.com
12     Jure Cuhalev <gandalf@owca.info>
13     John D'Agostino <john.dagostino@gmail.com>
14Index: docs/ref/models/fields.txt
15===================================================================
16--- docs/ref/models/fields.txt  (revision 16341)
17+++ docs/ref/models/fields.txt  (working copy)
18@@ -210,6 +210,10 @@
19 field will raise. Pass in a dictionary with keys matching the error messages you
20 want to override.
21 
22+Error message keys include ``null``, ``blank``, ``invalid``, ``invalid_choice``,
23+and ``unique``. Additional error message keys are specified for each field in
24+the `Field types`_ section below.
25+
26 ``help_text``
27 -------------
28 
29@@ -416,7 +420,8 @@
30     it's not just a default value that you can override.
31 
32 The admin represents this as an ``<input type="text">`` with a JavaScript
33-calendar, and a shortcut for "Today".
34+calendar, and a shortcut for "Today". Includes an additional ``invalid_date``
35+error message key.
36 
37 .. note::
38     As currently implemented, setting ``auto_now`` or ``auto_now_add`` to
39Index: tests/modeltests/validation/models.py
40===================================================================
41--- tests/modeltests/validation/models.py       (revision 16341)
42+++ tests/modeltests/validation/models.py       (working copy)
43@@ -78,3 +78,7 @@
44     slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
45     subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
46     posted = models.DateField(blank=True, null=True)
47+
48+class UniqueErrorsModel(models.Model):
49+    name = models.CharField(max_length=100, unique=True, error_messages={'unique': u'Custom unique name message.'})
50+    number = models.IntegerField(unique=True, error_messages={'unique': u'Custom unique number message.'})
51\ No newline at end of file
52Index: tests/modeltests/validation/test_unique.py
53===================================================================
54--- tests/modeltests/validation/test_unique.py  (revision 16341)
55+++ tests/modeltests/validation/test_unique.py  (working copy)
56@@ -7,7 +7,7 @@
57 from django.utils import unittest
58 
59 from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
60-    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
61+    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, UniqueErrorsModel)
62 
63 
64 class GetUniqueCheckTests(unittest.TestCase):
65@@ -149,3 +149,22 @@
66             self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
67         except:
68             self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
69+
70+    def test_unique_errors(self):
71+        m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10)
72+        m = UniqueErrorsModel(name='Some Name', number=11)
73+        try:
74+            m.full_clean()
75+        except ValidationError, e:
76+            self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']})
77+        except:
78+            self.fail('unique checks should catch this.')
79+
80+        m = UniqueErrorsModel(name='Some Other Name', number=10)
81+        try:
82+            m.full_clean()
83+        except ValidationError, e:
84+            self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']})
85+        except:
86+            self.fail('unique checks should catch this.')
87+           
88\ No newline at end of file
89Index: django/db/models/base.py
90===================================================================
91--- django/db/models/base.py    (revision 16341)
92+++ django/db/models/base.py    (working copy)
93@@ -782,9 +782,10 @@
94         # A unique field
95         if len(unique_check) == 1:
96             field_name = unique_check[0]
97-            field_label = capfirst(opts.get_field(field_name).verbose_name)
98+            field = opts.get_field(field_name)
99+            field_label = capfirst(field.verbose_name)
100             # Insert the error into the error dict, very sneaky
101-            return _(u"%(model_name)s with this %(field_label)s already exists.") %  {
102+            return field.error_messages['unique'] %  {
103                 'model_name': unicode(model_name),
104                 'field_label': unicode(field_label)
105             }
106Index: django/db/models/fields/__init__.py
107===================================================================
108--- django/db/models/fields/__init__.py (revision 16341)
109+++ django/db/models/fields/__init__.py (working copy)
110@@ -60,6 +60,7 @@
111         'invalid_choice': _(u'Value %r is not a valid choice.'),
112         'null': _(u'This field cannot be null.'),
113         'blank': _(u'This field cannot be blank.'),
114+        'unique': _(u'%(model_name)s with this %(field_label)s already exists.'),
115     }
116 
117     # Generic field type description, usually overriden by subclasses