summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@sixmedia.com>2014-02-04 01:31:27 +0700
committerTim Graham <timograham@gmail.com>2014-02-08 04:59:09 -0500
commit8847a0c601e4261823b1726b2db73eec2ac17940 (patch)
treead62fc5aca51cc9446ae68d7033691be68c87940 /tests/model_forms
parent65131911dba08dcc1451d71ae4d5101724d722f6 (diff)
Fixed #16192 -- Made unique error messages in ModelForm customizable.
Overriding the error messages now works for both unique fields, unique_together and unique_for_date. This patch changed the overriding logic to allow customizing NON_FIELD_ERRORS since previously only fields' errors were customizable. Refs #20199. Thanks leahculver for the suggestion.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/tests.py45
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index c5e16a2c14..c64bad5241 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -7,7 +7,7 @@ from unittest import skipUnless
import warnings
from django import forms
-from django.core.exceptions import FieldError
+from django.core.exceptions import FieldError, NON_FIELD_ERRORS
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.validators import ValidationError
from django.db import connection
@@ -792,6 +792,49 @@ class UniqueTest(TestCase):
"slug": "Django 1.0"}, instance=p)
self.assertTrue(form.is_valid())
+ def test_override_unique_message(self):
+ class CustomProductForm(ProductForm):
+ class Meta(ProductForm.Meta):
+ error_messages = {
+ 'slug': {
+ 'unique': "%(model_name)s's %(field_label)s not unique.",
+ }
+ }
+
+ Product.objects.create(slug='teddy-bear-blue')
+ form = CustomProductForm({'slug': 'teddy-bear-blue'})
+ self.assertEqual(len(form.errors), 1)
+ self.assertEqual(form.errors['slug'], ["Product's Slug not unique."])
+
+ def test_override_unique_together_message(self):
+ class CustomPriceForm(PriceForm):
+ class Meta(PriceForm.Meta):
+ error_messages = {
+ NON_FIELD_ERRORS: {
+ 'unique_together': "%(model_name)s's %(field_labels)s not unique.",
+ }
+ }
+
+ Price.objects.create(price=6.00, quantity=1)
+ form = CustomPriceForm({'price': '6.00', 'quantity': '1'})
+ self.assertEqual(len(form.errors), 1)
+ self.assertEqual(form.errors[NON_FIELD_ERRORS], ["Price's Price and Quantity not unique."])
+
+ def test_override_unique_for_date_message(self):
+ class CustomPostForm(PostForm):
+ class Meta(PostForm.Meta):
+ error_messages = {
+ 'title': {
+ 'unique_for_date': "%(model_name)s's %(field_label)s not unique for %(date_field_label)s date.",
+ }
+ }
+
+ Post.objects.create(title="Django 1.0 is released",
+ slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
+ form = CustomPostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
+ self.assertEqual(len(form.errors), 1)
+ self.assertEqual(form.errors['title'], ["Post's Title not unique for Posted date."])
+
class ModelToDictTests(TestCase):
"""