mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.
This commit is contained in:
parent
68a8996bdf
commit
500e01073a
29 changed files with 822 additions and 249 deletions
|
@ -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),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue