Ticket #4412: 4412-r7000.diff
File 4412-r7000.diff, 7.2 KB (added by , 17 years ago) |
---|
-
django/db/models/base.py
335 335 336 336 def _get_FIELD_display(self, field): 337 337 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) 339 345 340 346 def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs): 341 347 qn = connection.ops.quote_name -
django/newforms/fields.py
582 582 value = smart_unicode(value) 583 583 if value == u'': 584 584 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]) 586 592 if value not in valid_values: 587 593 raise ValidationError(self.error_messages['invalid_choice'] % {'value': value}) 588 594 return value … … 607 613 raise ValidationError(self.error_messages['invalid_list']) 608 614 new_value = [smart_unicode(val) for val in value] 609 615 # 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]) 611 623 for val in new_value: 612 624 if val not in valid_values: 613 625 raise ValidationError(self.error_messages['invalid_choice'] % {'value': val}) -
django/newforms/widgets.py
215 215 # Normalize to string. 216 216 str_value = force_unicode(value) 217 217 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>' % ( 221 231 escape(option_value), selected_html, 222 232 conditional_escape(force_unicode(option_label)))) 223 233 output.append(u'</select>') -
tests/regressiontests/forms/widgets.py
419 419 <option value="4">4</option> 420 420 </select> 421 421 422 Choices 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 422 449 # NullBooleanSelect Widget #################################################### 423 450 424 451 >>> w = NullBooleanSelect() -
docs/newforms.txt
1229 1229 * Error message keys: ``required``, ``invalid_choice`` 1230 1230 1231 1231 Takes 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. 1232 tuple) of 2-tuples to use as choices for this field. This argument accepts 1233 the same formats as the ``choices`` argument to a model field. See the 1234 `model API documentation on choices`_ for more details. 1233 1235 1236 .. _model API documentation on choices: ../model-api#choices 1237 1234 1238 ``DateField`` 1235 1239 ~~~~~~~~~~~~~ 1236 1240 -
docs/model-api.txt
568 568 class Foo(models.Model): 569 569 gender = models.CharField(max_length=1, choices=GENDER_CHOICES) 570 570 571 You can also collect your available choices into named groups, which will 572 be 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 588 The first element in each tuple is the name to apply to the group. The 589 second element is an iterable of 2-tuples, with each 2-tuple containing 590 a value and a human-readable name for an option. Grouped options may be 591 combined with ungrouped options within a single list (such as the 592 `unknown` option in this example). 593 571 594 For each model field that has ``choices`` set, Django will add a method to 572 595 retrieve the human-readable name for the field's current value. See 573 596 `get_FOO_display`_ in the database API documentation.