Ticket #4412: 4412-2.diff

File 4412-2.diff, 8.9 KB (added by Matt McClanahan <cardinal@…>, 17 years ago)
  • django/db/models/base.py

     
    317317
    318318    def _get_FIELD_display(self, field):
    319319        value = getattr(self, field.attname)
    320         return dict(field.choices).get(value, value)
     320        newchoices = []
     321        for choice in field.choices:
     322            if type(choice[1]) == list or type(choice[1]) == tuple:
     323                newchoices.extend(choice[1])
     324            else:
     325                newchoices.append(choice)
     326        return dict(newchoices).get(value, value)
    321327
    322328    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
    323329        op = is_next and '>' or '<'
  • tests/regressiontests/forms/tests.py

     
    392392<option value="4">4</option>
    393393</select>
    394394
     395Choices can be nested one level in order to create HTML optgroups:
     396>>> w = Select(choices=(('outer1', 'Outer 1'), ('Group 1', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))))
     397>>> print w.render('nestchoice', None)
     398<select name="nestchoice">
     399<option value="outer1">Outer 1</option>
     400<optgroup label="Group 1">
     401<option value="inner1">Inner 1</option>
     402<option value="inner2">Inner 2</option>
     403</optgroup>
     404</select>
     405>>> print w.render('nestchoice', 'outer1')
     406<select name="nestchoice">
     407<option value="outer1" selected="selected">Outer 1</option>
     408<optgroup label="Group 1">
     409<option value="inner1">Inner 1</option>
     410<option value="inner2">Inner 2</option>
     411</optgroup>
     412</select>
     413>>> print w.render('nestchoice', 'inner1')
     414<select name="nestchoice">
     415<option value="outer1">Outer 1</option>
     416<optgroup label="Group 1">
     417<option value="inner1" selected="selected">Inner 1</option>
     418<option value="inner2">Inner 2</option>
     419</optgroup>
     420</select>
     421
    395422# NullBooleanSelect Widget ####################################################
    396423
    397424>>> w = NullBooleanSelect()
     
    533560>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    534561u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>'
    535562
     563Choices can be nested one level in order to create HTML optgroups:
     564>>> w = SelectMultiple(choices=(('outer1', 'Outer 1'), ('Group 1', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))))
     565>>> print w.render('nestchoice', None)
     566<select multiple="multiple" name="nestchoice">
     567<option value="outer1">Outer 1</option>
     568<optgroup label="Group 1">
     569<option value="inner1">Inner 1</option>
     570<option value="inner2">Inner 2</option>
     571</optgroup>
     572</select>
     573>>> print w.render('nestchoice', ['outer1'])
     574<select multiple="multiple" name="nestchoice">
     575<option value="outer1" selected="selected">Outer 1</option>
     576<optgroup label="Group 1">
     577<option value="inner1">Inner 1</option>
     578<option value="inner2">Inner 2</option>
     579</optgroup>
     580</select>
     581>>> print w.render('nestchoice', ['inner1'])
     582<select multiple="multiple" name="nestchoice">
     583<option value="outer1">Outer 1</option>
     584<optgroup label="Group 1">
     585<option value="inner1" selected="selected">Inner 1</option>
     586<option value="inner2">Inner 2</option>
     587</optgroup>
     588</select>
     589>>> print w.render('nestchoice', ['outer1', 'inner2'])
     590<select multiple="multiple" name="nestchoice">
     591<option value="outer1" selected="selected">Outer 1</option>
     592<optgroup label="Group 1">
     593<option value="inner1">Inner 1</option>
     594<option value="inner2" selected="selected">Inner 2</option>
     595</optgroup>
     596</select>
     597
    536598# RadioSelect Widget ##########################################################
    537599
    538600>>> w = RadioSelect()
  • django/newforms/fields.py

     
    427427        value = smart_unicode(value)
    428428        if value == u'':
    429429            return value
    430         valid_values = set([str(k) for k, v in self.choices])
     430        valid_values = []
     431        for k, v in self.choices:
     432            if type(v) == tuple or type(v) == list:
     433                for k2, v2 in v:
     434                    valid_values.append(str(k2))
     435                continue
     436            valid_values.append(str(k))
     437        valid_values = set(valid_values)
    431438        if value not in valid_values:
    432439            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
    433440        return value
     
    451458            val = smart_unicode(val)
    452459            new_value.append(val)
    453460        # Validate that each value in the value list is in self.choices.
    454         valid_values = set([smart_unicode(k) for k, v in self.choices])
     461        valid_values = []
     462        for k, v in self.choices:
     463            if type(v) == tuple or type(v) == list:
     464                for k2, v2 in v:
     465                    valid_values.append(smart_unicode(k2))
     466                continue
     467            valid_values.append(smart_unicode(k))
     468        valid_values = set(valid_values)
    455469        for val in new_value:
    456470            if val not in valid_values:
    457471                raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % val)
  • django/newforms/widgets.py

     
    168168        output = [u'<select%s>' % flatatt(final_attrs)]
    169169        str_value = smart_unicode(value) # Normalize to string.
    170170        for option_value, option_label in chain(self.choices, choices):
     171            if type(option_label) == list or type(option_label) == tuple:
     172                output.append(u'<optgroup label="%s">' % escape(smart_unicode(option_value)))
     173                for group_option_value, group_option_label in option_label:
     174                    group_option_value = smart_unicode(group_option_value)
     175                    selected_html = (group_option_value == str_value) and u' selected="selected"' or ''
     176                    output.append(u'<option value="%s"%s>%s</option>' % (escape(group_option_value), selected_html, escape(smart_unicode(group_option_label))))
     177                output.append(u'</optgroup>')
     178                continue
    171179            option_value = smart_unicode(option_value)
    172180            selected_html = (option_value == str_value) and u' selected="selected"' or ''
    173181            output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(smart_unicode(option_label))))
     
    205213        output = [u'<select multiple="multiple"%s>' % flatatt(final_attrs)]
    206214        str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
    207215        for option_value, option_label in chain(self.choices, choices):
     216            if type(option_label) == list or type(option_label) == tuple:
     217                output.append(u'<optgroup label="%s">' % escape(smart_unicode(option_value)))
     218                for group_option_value, group_option_label in option_label:
     219                    group_option_value = smart_unicode(group_option_value)
     220                    selected_html = (group_option_value in str_values) and ' selected="selected"' or ''
     221                    output.append(u'<option value="%s"%s>%s</option>' % (escape(group_option_value), selected_html, escape(smart_unicode(group_option_label))))
     222                output.append(u'</optgroup>')
     223                continue
    208224            option_value = smart_unicode(option_value)
    209225            selected_html = (option_value in str_values) and ' selected="selected"' or ''
    210226            output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(smart_unicode(option_label))))
  • docs/model-api.txt

     
    537537    class Foo(models.Model):
    538538        gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
    539539
     540The choices list may also be nested one level::
     541
     542    MEDIA_CHOICES = (
     543        ('Audio', (
     544                ('vinyl', 'Vinyl'),
     545                ('cd', 'CD'),
     546            )
     547        ),
     548        ('Video', (
     549                ('vhs', 'VHS Tape'),
     550                ('dvd', 'DVD'),
     551            )
     552        ),
     553        ('unknown', 'Unknown'),
     554    )
     555
    540556For each model field that has ``choices`` set, Django will add a method to
    541557retrieve the human-readable name for the field's current value. See
    542558`get_FOO_display`_ in the database API documentation.
Back to Top