summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2020-10-17 16:17:00 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-17 21:06:57 +0200
commit72a17c919bf9071ca34b00cd72b0084e5c6da8f6 (patch)
treead89533edabd77efb98ae0171346f71c2baeedfa
parent301bca9394cd977bc085c5cc50bd0ef9f6af52d7 (diff)
downloaddjango-72a17c919bf9071ca34b00cd72b0084e5c6da8f6.tar.gz
[3.0.x] Fixed #32110 -- Doc'd and tested enumerations for ChoiceField.choices.
Backport of 7f85498eef1d8fcc52e4fb70df8041f5452d405a from master
-rw-r--r--docs/ref/forms/fields.txt12
-rw-r--r--tests/forms_tests/field_tests/test_choicefield.py12
2 files changed, 18 insertions, 6 deletions
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index 5f53becce8..89b9c697f1 100644
--- a/docs/ref/forms/fields.txt
+++ b/docs/ref/forms/fields.txt
@@ -408,12 +408,12 @@ For each field, we describe the default widget used if you don't specify
.. attribute:: choices
Either an :term:`iterable` of 2-tuples to use as choices for this
- field, or a callable that returns such an iterable. This argument
- accepts the same formats as the ``choices`` argument to a model field.
- See the :ref:`model field reference documentation on choices
- <field-choices>` for more details. If the argument is a callable, it is
- evaluated each time the field's form is initialized. Defaults to an
- empty list.
+ field, :ref:`enumeration <field-choices-enum-types>` choices, or a
+ callable that returns such an iterable. This argument accepts the same
+ formats as the ``choices`` argument to a model field. See the
+ :ref:`model field reference documentation on choices <field-choices>`
+ for more details. If the argument is a callable, it is evaluated each
+ time the field's form is initialized. Defaults to an empty list.
``TypedChoiceField``
--------------------
diff --git a/tests/forms_tests/field_tests/test_choicefield.py b/tests/forms_tests/field_tests/test_choicefield.py
index 465cfd83a8..c27d192108 100644
--- a/tests/forms_tests/field_tests/test_choicefield.py
+++ b/tests/forms_tests/field_tests/test_choicefield.py
@@ -1,3 +1,4 @@
+from django.db import models
from django.forms import ChoiceField, Form, ValidationError
from django.test import SimpleTestCase
@@ -86,3 +87,14 @@ class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
'<select id="id_f" name="f" disabled><option value="J">John</option>'
'<option value="P">Paul</option></select>'
)
+
+ def test_choicefield_enumeration(self):
+ class FirstNames(models.TextChoices):
+ JOHN = 'J', 'John'
+ PAUL = 'P', 'Paul'
+
+ f = ChoiceField(choices=FirstNames.choices)
+ self.assertEqual(f.clean('J'), 'J')
+ msg = "'Select a valid choice. 3 is not one of the available choices.'"
+ with self.assertRaisesMessage(ValidationError, msg):
+ f.clean('3')