Ticket #4412: 4412.diff

File 4412.diff, 8.9 KB (added by Tai Lee <real.human@…>, 11 years ago)

updated to work on trunk, after unicode changes.

  • 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 '<'
  • 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                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(gettext(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(gettext(u'Select a valid choice. %s is not one of the available choices.') % val)
  • django/newforms/widgets.py

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

     
    542542    class Foo(models.Model):
    543543        gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
    544544
     545The choices list may also be nested one level::
     546
     547    MEDIA_CHOICES = (
     548        ('Audio', (
     549                ('vinyl', 'Vinyl'),
     550                ('cd', 'CD'),
     551            )
     552        ),
     553        ('Video', (
     554                ('vhs', 'VHS Tape'),
     555                ('dvd', 'DVD'),
     556            )
     557        ),
     558        ('unknown', 'Unknown'),
     559    )
     560
    545561For each model field that has ``choices`` set, Django will add a method to
    546562retrieve the human-readable name for the field's current value. See
    547563`get_FOO_display`_ in the database API documentation.
Back to Top