Index: django/db/models/base.py
===================================================================
--- django/db/models/base.py (revision 6025)
+++ django/db/models/base.py (working copy)
@@ -324,7 +324,13 @@
def _get_FIELD_display(self, field):
value = getattr(self, field.attname)
- return force_unicode(dict(field.choices).get(value, value), strings_only=True)
+ flatchoices = []
+ for choice in field.choices:
+ if type(choice[1]) in (list, tuple):
+ flatchoices.extend(choice[1])
+ else:
+ flatchoices.append(choice)
+ return force_unicode(dict(flatchoices).get(value, value), strings_only=True)
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
qn = connection.ops.quote_name
Index: django/newforms/fields.py
===================================================================
--- django/newforms/fields.py (revision 6025)
+++ django/newforms/fields.py (working copy)
@@ -473,7 +473,13 @@
value = smart_unicode(value)
if value == u'':
return value
- valid_values = set([smart_unicode(k) for k, v in self.choices])
+ valid_values = []
+ for k, v in self.choices:
+ if type(v) in (tuple, list):
+ valid_values.extend([k2[0] for k2 in v])
+ else:
+ valid_values.append(k)
+ valid_values = set([smart_unicode(v) for v in valid_values])
if value not in valid_values:
raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
return value
@@ -494,7 +500,13 @@
raise ValidationError(ugettext(u'Enter a list of values.'))
new_value = [smart_unicode(val) for val in value]
# Validate that each value in the value list is in self.choices.
- valid_values = set([smart_unicode(k) for k, v in self.choices])
+ valid_values = []
+ for k, v in self.choices:
+ if type(v) in (tuple, list):
+ valid_values.extend([k2[0] for k2 in v])
+ else:
+ valid_values.append(k)
+ valid_values = set([smart_unicode(v) for v in valid_values])
for val in new_value:
if val not in valid_values:
raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
Index: django/newforms/widgets.py
===================================================================
--- django/newforms/widgets.py (revision 6025)
+++ django/newforms/widgets.py (working copy)
@@ -174,9 +174,17 @@
output = [u'')
return u'\n'.join(output)
@@ -211,9 +219,17 @@
output = [u'')
return u'\n'.join(output)
Index: tests/regressiontests/forms/tests.py
===================================================================
--- tests/regressiontests/forms/tests.py (revision 6025)
+++ tests/regressiontests/forms/tests.py (working copy)
@@ -395,6 +395,33 @@
+Choices can be nested one level in order to create HTML optgroups:
+>>> w = Select(choices=(('outer1', 'Outer 1'), ('Group 1', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))))
+>>> print w.render('nestchoice', None)
+
+>>> print w.render('nestchoice', 'outer1')
+
+>>> print w.render('nestchoice', 'inner1')
+
+
# NullBooleanSelect Widget ####################################################
>>> w = NullBooleanSelect()
@@ -536,6 +563,41 @@
>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
u''
+Choices can be nested one level in order to create HTML optgroups:
+>>> w = SelectMultiple(choices=(('outer1', 'Outer 1'), ('Group 1', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))))
+>>> print w.render('nestchoice', None)
+
+>>> print w.render('nestchoice', ['outer1'])
+
+>>> print w.render('nestchoice', ['inner1'])
+
+>>> print w.render('nestchoice', ['outer1', 'inner2'])
+
+
# RadioSelect Widget ##########################################################
>>> w = RadioSelect()
Index: docs/newforms.txt
===================================================================
--- docs/newforms.txt (revision 6025)
+++ docs/newforms.txt (working copy)
@@ -1083,8 +1083,12 @@
* Validates that the given value exists in the list of choices.
Takes one extra argument, ``choices``, which is an iterable (e.g., a list or
-tuple) of 2-tuples to use as choices for this field.
+tuple) of 2-tuples to use as choices for this field. This argument accepts
+the same formats as the ``choices`` argument to a model field. See the
+`model API documentation on choices`_ for more details.
+.. _model API documentation on choices: ../model-api#choices
+
``DateField``
~~~~~~~~~~~~~
Index: docs/model-api.txt
===================================================================
--- docs/model-api.txt (revision 6025)
+++ docs/model-api.txt (working copy)
@@ -552,6 +552,29 @@
class Foo(models.Model):
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
+You can also collect your available choices into named groups, which will
+be used for display 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.