Ticket #12304: 12304-ensure-readable-label-in-unique-validation_error-messages.diff
File 12304-ensure-readable-label-in-unique-validation_error-messages.diff, 5.0 KB (added by , 15 years ago) |
---|
-
django/forms/models.py
diff -r da58c69195c6 django/forms/models.py
a b 9 9 from django.utils.translation import ugettext_lazy as _, ugettext 10 10 11 11 from util import ValidationError, ErrorList 12 from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS 12 from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS, pretty_name 13 13 from fields import Field, ChoiceField, IntegerField, EMPTY_VALUES 14 14 from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput 15 15 from widgets import media_property … … 373 373 if len(unique_check) == 1: 374 374 field_name = unique_check[0] 375 375 field_label = self.fields[field_name].label 376 if field_label is None: 377 field_label = pretty_name(field_name) 376 378 # Insert the error into the error dict, very sneaky 377 379 return _(u"%(model_name)s with this %(field_label)s already exists.") % { 378 380 'model_name': unicode(model_name), … … 380 382 } 381 383 # unique_together 382 384 else: 383 field_labels = [self.fields[field_name].label for field_name in unique_check] 385 def pretty_label(fld_name): 386 label = self.fields[field_name].label 387 if label is None: 388 label = pretty_name(fld_name) 389 return label 390 field_labels = [pretty_label(field_name) for field_name in unique_check] 384 391 field_labels = get_text_list(field_labels, _('and')) 385 392 return _(u"%(model_name)s with this %(field_label)s already exists.") % { 386 393 'model_name': unicode(model_name), -
tests/regressiontests/model_forms_regress/models.py
diff -r da58c69195c6 tests/regressiontests/model_forms_regress/models.py
a b 37 37 38 38 class CustomFF(models.Model): 39 39 f = CustomFileField(upload_to='unused', blank=True) 40 41 class Edition(models.Model): 42 author = models.ForeignKey(Person) 43 publication = models.ForeignKey(Publication) 44 edition = models.IntegerField() 45 isbn = models.CharField(max_length=13, unique=True) 46 47 class Meta: 48 unique_together = (('author', 'publication'), ('publication', 'edition'),) 49 -
tests/regressiontests/model_forms_regress/tests.py
diff -r da58c69195c6 tests/regressiontests/model_forms_regress/tests.py
a b 6 6 from django.conf import settings 7 7 from django.test import TestCase 8 8 9 from models import Person, Triple, FilePathModel, Article, Publication, CustomFF 9 from models import Person, Triple, FilePathModel, Article, Publication, CustomFF, Edition 10 10 11 11 class ModelMultipleChoiceFieldTests(TestCase): 12 12 … … 96 96 class CustomFieldSaveTests(TestCase): 97 97 def test_save(self): 98 98 "Regression for #11149: save_form_data should be called only once" 99 99 100 100 # It's enough that the form saves without error -- the custom save routine will 101 101 # generate an AssertionError if it is called more than once during save. 102 102 form = CFFForm(data = {'f': None}) 103 form.save() 104 No newline at end of file 103 form.save() 104 105 106 class EditionForm(forms.ModelForm): 107 author = forms.ModelChoiceField(queryset=Person.objects.all()) 108 publication = forms.ModelChoiceField(queryset=Publication.objects.all()) 109 edition = forms.IntegerField() 110 isbn = forms.CharField(max_length=13) 111 112 class Meta: 113 model = Edition 114 115 class UniqueErrorsTests(TestCase): 116 117 def setUp(self): 118 self.author1 = Person.objects.create(name=u'Author #1') 119 self.author2 = Person.objects.create(name=u'Author #2') 120 self.pub1 = Publication.objects.create(title='Pub #1', date_published=date(2000, 10, 31)) 121 self.pub2 = Publication.objects.create(title='Pub #2', date_published=date(2004, 1, 5)) 122 form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161484100'}) 123 form.save() 124 125 def test_unique_error_message(self): 126 form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub2.pk, 'edition': 1, 'isbn': '9783161484100'}) 127 self.assertEquals(form.errors, {'isbn': [u'Edition with this Isbn already exists.']}) 128 129 def test_unique_together_error_message(self): 130 form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 2, 'isbn': '9783161489999'}) 131 self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']}) 132 form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'}) 133 self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']})