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 ramiro, 6 years ago)
  • django/forms/models.py

    diff -r da58c69195c6 django/forms/models.py
    a b  
    99from django.utils.translation import ugettext_lazy as _, ugettext
    1010
    1111from util import ValidationError, ErrorList
    12 from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS
     12from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS, pretty_name
    1313from fields import Field, ChoiceField, IntegerField, EMPTY_VALUES
    1414from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput
    1515from widgets import media_property
     
    373373        if len(unique_check) == 1:
    374374            field_name = unique_check[0]
    375375            field_label = self.fields[field_name].label
     376            if field_label is None:
     377                field_label = pretty_name(field_name)
    376378            # Insert the error into the error dict, very sneaky
    377379            return _(u"%(model_name)s with this %(field_label)s already exists.") %  {
    378380                'model_name': unicode(model_name),
     
    380382            }
    381383        # unique_together
    382384        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]
    384391            field_labels = get_text_list(field_labels, _('and'))
    385392            return _(u"%(model_name)s with this %(field_label)s already exists.") %  {
    386393                'model_name': unicode(model_name),
  • tests/regressiontests/model_forms_regress/models.py

    diff -r da58c69195c6 tests/regressiontests/model_forms_regress/models.py
    a b  
    3737
    3838class CustomFF(models.Model):
    3939    f = CustomFileField(upload_to='unused', blank=True)
     40
     41class 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  
    66from django.conf import settings
    77from django.test import TestCase
    88
    9 from models import Person, Triple, FilePathModel, Article, Publication, CustomFF
     9from models import Person, Triple, FilePathModel, Article, Publication, CustomFF, Edition
    1010
    1111class ModelMultipleChoiceFieldTests(TestCase):
    1212
     
    9696class CustomFieldSaveTests(TestCase):
    9797    def test_save(self):
    9898        "Regression for #11149: save_form_data should be called only once"
    99        
     99
    100100        # It's enough that the form saves without error -- the custom save routine will
    101101        # generate an AssertionError if it is called more than once during save.
    102102        form = CFFForm(data = {'f': None})
    103         form.save()
    104  No newline at end of file
     103        form.save()
     104
     105
     106class 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
     115class 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.']})
Back to Top