Ticket #4412: 4412-3.diff

File 4412-3.diff, 9.0 KB (added by Matt McClanahan <cardinal@…>, 11 years ago)

Updated for the unicode branch merge

  • django/db/models/base.py

     
    321321
    322322    def _get_FIELD_display(self, field):
    323323        value = getattr(self, field.attname)
    324         return force_unicode(dict(field.choices).get(value, value), strings_only=True)
     324        newchoices = []
     325        for choice in field.choices:
     326            if type(choice[1]) == list or type(choice[1]) == tuple:
     327                newchoices.extend(c2[0] for c2 in choice[1])
     328            else:
     329                newchoices.append(choice)
     330        return force_unicode(dict(newchoices).get(value, value), strings_only=True)
    325331
    326332    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
    327333        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([smart_unicode(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                valid_values.extend([k2[0] for k2 in v])
     434                continue
     435            valid_values.append(k)
     436        valid_values = set([smart_unicode(v) for v in valid_values])
    431437        if value not in valid_values:
    432438            raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
    433439        return value
     
    451457            val = smart_unicode(val)
    452458            new_value.append(val)
    453459        # 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])
     460        valid_values = []
     461        for k, v in self.choices:
     462            if type(v) == tuple or type(v) == list:
     463                valid_values.extend([k2[0] for k2 in v])
     464                continue
     465            valid_values.append(k)
     466        valid_values = set([smart_unicode(v) for v in valid_values])
    455467        for val in new_value:
    456468            if val not in valid_values:
    457469                raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
  • django/newforms/widgets.py

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

     
    547547    class Foo(models.Model):
    548548        gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
    549549
     550The choices list may also be nested one level::
     551
     552    MEDIA_CHOICES = (
     553        ('Audio', (
     554                ('vinyl', 'Vinyl'),
     555                ('cd', 'CD'),
     556            )
     557        ),
     558        ('Video', (
     559                ('vhs', 'VHS Tape'),
     560                ('dvd', 'DVD'),
     561            )
     562        ),
     563        ('unknown', 'Unknown'),
     564    )
     565
    550566For each model field that has ``choices`` set, Django will add a method to
    551567retrieve the human-readable name for the field's current value. See
    552568`get_FOO_display`_ in the database API documentation.
Back to Top