Added ModelChoiceField test for validation with to_field_name.

This commit is contained in:
Etienne Chové 2019-10-11 13:00:06 +02:00 committed by Mariusz Felisiak
parent 7d1bf29977
commit a12f9cd95a
1 changed files with 4 additions and 0 deletions

View File

@ -55,6 +55,10 @@ class ModelChoiceFieldTests(TestCase):
with self.assertRaisesMessage(ValidationError, msg): with self.assertRaisesMessage(ValidationError, msg):
f.clean(c4.id) f.clean(c4.id)
def test_clean_to_field_name(self):
f = forms.ModelChoiceField(Category.objects.all(), to_field_name='slug')
self.assertEqual(f.clean(self.c1.slug), self.c1)
def test_choices(self): def test_choices(self):
f = forms.ModelChoiceField(Category.objects.filter(pk=self.c1.id), required=False) f = forms.ModelChoiceField(Category.objects.filter(pk=self.c1.id), required=False)
self.assertIsNone(f.clean('')) self.assertIsNone(f.clean(''))