summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorEtienne Chové <chove@crans.org>2019-03-13 10:02:50 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-10-11 13:28:09 +0200
commite7cdb0cd7eb5eb677af8dae7bfc6845186f861b0 (patch)
tree0814b43200e1b54cb1995b46a8a03609d0f6dc45 /tests/model_forms
parenta12f9cd95a24573612daaf844ec97d4aed12446d (diff)
Fixed #30014 -- Fixed ModelChoiceField validation when initial value is a model instance.
Thanks Carlton Gibson for reviews.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/test_modelchoicefield.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py
index 74ef14aa73..0591c425fe 100644
--- a/tests/model_forms/test_modelchoicefield.py
+++ b/tests/model_forms/test_modelchoicefield.py
@@ -55,9 +55,18 @@ class ModelChoiceFieldTests(TestCase):
with self.assertRaisesMessage(ValidationError, msg):
f.clean(c4.id)
+ def test_clean_model_instance(self):
+ f = forms.ModelChoiceField(Category.objects.all())
+ self.assertEqual(f.clean(self.c1), self.c1)
+ # An instance of incorrect model.
+ msg = "['Select a valid choice. That choice is not one of the available choices.']"
+ with self.assertRaisesMessage(ValidationError, msg):
+ f.clean(Book.objects.create())
+
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)
+ self.assertEqual(f.clean(self.c1), self.c1)
def test_choices(self):
f = forms.ModelChoiceField(Category.objects.filter(pk=self.c1.id), required=False)
@@ -194,6 +203,16 @@ class ModelChoiceFieldTests(TestCase):
field = forms.ModelChoiceField(Author.objects.all(), disabled=True)
self.assertIs(field.has_changed('x', 'y'), False)
+ def test_disabled_modelchoicefield_initial_model_instance(self):
+ class ModelChoiceForm(forms.Form):
+ categories = forms.ModelChoiceField(
+ Category.objects.all(),
+ disabled=True,
+ initial=self.c1,
+ )
+
+ self.assertTrue(ModelChoiceForm(data={'categories': self.c1.pk}).is_valid())
+
def test_disabled_multiplemodelchoicefield(self):
class ArticleForm(forms.ModelForm):
categories = forms.ModelMultipleChoiceField(Category.objects.all(), required=False)