Ticket #4412: 4412-r7000.diff

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

Updated to r7000.

  • django/db/models/base.py

     
    335335
    336336    def _get_FIELD_display(self, field):
    337337        value = getattr(self, field.attname)
    338         return force_unicode(dict(field.choices).get(value, value), strings_only=True)
     338        flatchoices = []
     339        for choice in field.choices:
     340            if type(choice[1]) in (list, tuple):
     341                flatchoices.extend(choice[1])
     342            else:
     343                flatchoices.append(choice)
     344        return force_unicode(dict(flatchoices).get(value, value), strings_only=True)
    339345
    340346    def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
    341347        qn = connection.ops.quote_name
  • django/newforms/fields.py

     
    582582        value = smart_unicode(value)
    583583        if value == u'':
    584584            return value
    585         valid_values = set([smart_unicode(k) for k, v in self.choices])
     585        valid_values = []
     586        for k, v in self.choices:
     587            if type(v) in (tuple, list):
     588                valid_values.extend([k2[0] for k2 in v])
     589            else:
     590                valid_values.append(k)
     591        valid_values = set([smart_unicode(v) for v in valid_values])       
    586592        if value not in valid_values:
    587593            raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
    588594        return value
     
    607613            raise ValidationError(self.error_messages['invalid_list'])
    608614        new_value = [smart_unicode(val) for val in value]
    609615        # Validate that each value in the value list is in self.choices.
    610         valid_values = set([smart_unicode(k) for k, v in self.choices])
     616        valid_values = []
     617        for k, v in self.choices:
     618            if type(v) in (tuple, list):
     619                valid_values.extend([k2[0] for k2 in v])
     620            else:
     621                valid_values.append(k)
     622        valid_values = set([smart_unicode(v) for v in valid_values])       
    611623        for val in new_value:
    612624            if val not in valid_values:
    613625                raise ValidationError(self.error_messages['invalid_choice'] % {'value': val})
  • django/newforms/widgets.py

     
    215215        # Normalize to string.
    216216        str_value = force_unicode(value)
    217217        for option_value, option_label in chain(self.choices, choices):
    218             option_value = force_unicode(option_value)
    219             selected_html = (option_value == str_value) and u' selected="selected"' or ''
    220             output.append(u'<option value="%s"%s>%s</option>' % (
     218            if type(option_label) in (list, tuple):
     219                output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
     220                for group_option_value, group_option_label in option_label:
     221                    group_option_value = force_unicode(group_option_value)
     222                    selected_html = (group_option_value == str_value) and u' selected="selected"' or ''
     223                    output.append(u'<option value="%s"%s>%s</option>' % (
     224                        escape(group_option_value), selected_html,
     225                        conditional_escape(force_unicode(group_option_label))))
     226                output.append(u'</optgroup>')
     227            else:
     228                option_value = force_unicode(option_value)
     229                selected_html = (option_value == str_value) and u' selected="selected"' or ''
     230                output.append(u'<option value="%s"%s>%s</option>' % (
    221231                    escape(option_value), selected_html,
    222232                    conditional_escape(force_unicode(option_label))))
    223233        output.append(u'</select>')
  • tests/regressiontests/forms/widgets.py

     
    419419<option value="4">4</option>
    420420</select>
    421421
     422Choices can be nested one level in order to create HTML optgroups:
     423>>> w = Select(choices=(('outer1', 'Outer 1'), ('Group 1', (('inner1', 'Inner 1'), ('inner2', 'Inner 2')))))
     424>>> print w.render('nestchoice', None)
     425<select name="nestchoice">
     426<option value="outer1">Outer 1</option>
     427<optgroup label="Group 1">
     428<option value="inner1">Inner 1</option>
     429<option value="inner2">Inner 2</option>
     430</optgroup>
     431</select>
     432>>> print w.render('nestchoice', 'outer1')
     433<select name="nestchoice">
     434<option value="outer1" selected="selected">Outer 1</option>
     435<optgroup label="Group 1">
     436<option value="inner1">Inner 1</option>
     437<option value="inner2">Inner 2</option>
     438</optgroup>
     439</select>
     440>>> print w.render('nestchoice', 'inner1')
     441<select name="nestchoice">
     442<option value="outer1">Outer 1</option>
     443<optgroup label="Group 1">
     444<option value="inner1" selected="selected">Inner 1</option>
     445<option value="inner2">Inner 2</option>
     446</optgroup>
     447</select>
     448
    422449# NullBooleanSelect Widget ####################################################
    423450
    424451>>> w = NullBooleanSelect()
  • docs/newforms.txt

     
    12291229    * Error message keys: ``required``, ``invalid_choice``
    12301230
    12311231Takes one extra argument, ``choices``, which is an iterable (e.g., a list or
    1232 tuple) of 2-tuples to use as choices for this field.
     1232tuple) of 2-tuples to use as choices for this field. This argument accepts
     1233the same formats as the ``choices`` argument to a model field. See the
     1234`model API documentation on choices`_ for more details.
    12331235
     1236.. _model API documentation on choices: ../model-api#choices
     1237
    12341238``DateField``
    12351239~~~~~~~~~~~~~
    12361240
  • docs/model-api.txt

     
    568568    class Foo(models.Model):
    569569        gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    570570
     571You can also collect your available choices into named groups, which will
     572be used for display purposes::
     573
     574    MEDIA_CHOICES = (
     575        ('Audio', (
     576                ('vinyl', 'Vinyl'),
     577                ('cd', 'CD'),
     578            )
     579        ),
     580        ('Video', (
     581                ('vhs', 'VHS Tape'),
     582                ('dvd', 'DVD'),
     583            )
     584        ),
     585        ('unknown', 'Unknown'),
     586    )
     587
     588The first element in each tuple is the name to apply to the group. The
     589second element is an iterable of 2-tuples, with each 2-tuple containing
     590a value and a human-readable name for an option. Grouped options may be
     591combined with ungrouped options within a single list (such as the
     592`unknown` option in this example).
     593
    571594For each model field that has ``choices`` set, Django will add a method to
    572595retrieve the human-readable name for the field's current value. See
    573596`get_FOO_display`_ in the database API documentation.
Back to Top