mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #4412 -- Added support for optgroups, both in the model when defining choices, and in the form field and widgets when the optgroups are displayed. Thanks to Matt McClanahan <cardinal@dodds.net>, Tai Lee <real.human@mrmachine.net> and SmileyChris for their contributions at various stages in the life of this ticket.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7977 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
b5b0febc4c
commit
649463dd34
8 changed files with 204 additions and 40 deletions
|
@ -554,6 +554,29 @@ or outside your model class altogether::
|
|||
class Foo(models.Model):
|
||||
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
||||
|
||||
You can also collect your available choices into named groups that can
|
||||
be used for organizational purposes::
|
||||
|
||||
MEDIA_CHOICES = (
|
||||
('Audio', (
|
||||
('vinyl', 'Vinyl'),
|
||||
('cd', 'CD'),
|
||||
)
|
||||
),
|
||||
('Video', (
|
||||
('vhs', 'VHS Tape'),
|
||||
('dvd', 'DVD'),
|
||||
)
|
||||
),
|
||||
('unknown', 'Unknown'),
|
||||
)
|
||||
|
||||
The first element in each tuple is the name to apply to the group. The
|
||||
second element is an iterable of 2-tuples, with each 2-tuple containing
|
||||
a value and a human-readable name for an option. Grouped options may be
|
||||
combined with ungrouped options within a single list (such as the
|
||||
`unknown` option in this example).
|
||||
|
||||
For each model field that has ``choices`` set, Django will add a method to
|
||||
retrieve the human-readable name for the field's current value. See
|
||||
`get_FOO_display`_ in the database API documentation.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue