[3.0.x] Added ModelChoiceField test for validation with to_field_name.

Backport of a12f9cd95a from master
This commit is contained in:
Etienne Chové 2019-10-11 13:00:06 +02:00 committed by Mariusz Felisiak
parent cf7ef5d258
commit 82ba905db3
1 changed files with 4 additions and 0 deletions

View File

@ -55,6 +55,10 @@ class ModelChoiceFieldTests(TestCase):
with self.assertRaisesMessage(ValidationError, msg):
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):
f = forms.ModelChoiceField(Category.objects.filter(pk=self.c1.id), required=False)
self.assertIsNone(f.clean(''))