1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.

This commit is contained in:
Nick Pope
2023-08-31 02:57:40 +01:00
committed by GitHub
parent 68a8996bdf
commit 500e01073a
29 changed files with 822 additions and 249 deletions

View File

@@ -510,8 +510,9 @@ For each field, we describe the default widget used if you don't specify
.. versionchanged:: 5.0
Support for using :ref:`enumeration types <field-choices-enum-types>`
directly in the ``choices`` was added.
Support for mappings and using
:ref:`enumeration types <field-choices-enum-types>` directly in
``choices`` was added.
``DateField``
-------------

View File

@@ -58,11 +58,11 @@ widget on the field. In the following example, the
from django import forms
BIRTH_YEAR_CHOICES = ["1980", "1981", "1982"]
FAVORITE_COLORS_CHOICES = [
("blue", "Blue"),
("green", "Green"),
("black", "Black"),
]
FAVORITE_COLORS_CHOICES = {
"blue": "Blue",
"green": "Green",
"black": "Black",
}
class SimpleForm(forms.Form):
@@ -95,7 +95,7 @@ example:
.. code-block:: pycon
>>> from django import forms
>>> CHOICES = [("1", "First"), ("2", "Second")]
>>> CHOICES = {"1": "First", "2": "Second"}
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
>>> choice_field.choices
[('1', 'First'), ('2', 'Second')]
@@ -458,9 +458,9 @@ foundation for custom widgets.
class DateSelectorWidget(forms.MultiWidget):
def __init__(self, attrs=None):
days = [(day, day) for day in range(1, 32)]
months = [(month, month) for month in range(1, 13)]
years = [(year, year) for year in [2018, 2019, 2020]]
days = {day: day for day in range(1, 32)}
months = {month: month for month in range(1, 13)}
years = {year: year for year in [2018, 2019, 2020]}
widgets = [
forms.Select(attrs=attrs, choices=days),
forms.Select(attrs=attrs, choices=months),