mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #32110 -- Doc'd and tested enumerations for ChoiceField.choices.
This commit is contained in:
parent
af87574a3c
commit
7f85498eef
@ -410,12 +410,13 @@ For each field, we describe the default widget used if you don't specify
|
|||||||
.. attribute:: choices
|
.. attribute:: choices
|
||||||
|
|
||||||
Either an :term:`iterable` of 2-tuples to use as choices for this
|
Either an :term:`iterable` of 2-tuples to use as choices for this
|
||||||
field, or a callable that returns such an iterable. This argument
|
field, :ref:`enumeration <field-choices-enum-types>` choices, or a
|
||||||
accepts the same formats as the ``choices`` argument to a model field.
|
callable that returns such an iterable. This argument accepts the same
|
||||||
See the :ref:`model field reference documentation on choices
|
formats as the ``choices`` argument to a model field. See the
|
||||||
<field-choices>` for more details. If the argument is a callable, it is
|
:ref:`model field reference documentation on choices <field-choices>`
|
||||||
evaluated each time the field's form is initialized, in addition to
|
for more details. If the argument is a callable, it is evaluated each
|
||||||
during rendering. Defaults to an empty list.
|
time the field's form is initialized, in addition to during rendering.
|
||||||
|
Defaults to an empty list.
|
||||||
|
|
||||||
``TypedChoiceField``
|
``TypedChoiceField``
|
||||||
--------------------
|
--------------------
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.db import models
|
||||||
from django.forms import ChoiceField, Form
|
from django.forms import ChoiceField, Form
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
@ -87,3 +88,14 @@ class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|||||||
'<select id="id_f" name="f" disabled><option value="J">John</option>'
|
'<select id="id_f" name="f" disabled><option value="J">John</option>'
|
||||||
'<option value="P">Paul</option></select>'
|
'<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')
|
||||||
|
Loading…
Reference in New Issue
Block a user