Changeset 8102
- Timestamp:
- 07/27/08 02:22:39 (4 months ago)
- Files:
-
- django/trunk/django/db/models/fields/__init__.py (modified) (5 diffs)
- django/trunk/tests/modeltests/choices/models.py (modified) (1 diff)
- django/trunk/tests/regressiontests/model_fields/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/fields/__init__.py
r8050 r8102 289 289 field_objs = [oldforms.SelectField] 290 290 291 params['choices'] = self. flatchoices291 params['choices'] = self.get_flatchoices() 292 292 else: 293 293 field_objs = self.get_manipulator_field_objs() … … 363 363 364 364 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): 365 "Returns a list of tuples used as SelectField choices for this field." 365 """Returns choices with a default blank choices included, for use 366 as SelectField choices for this field.""" 366 367 first_choice = include_blank and blank_choice or [] 367 368 if self.choices: … … 377 378 return self.get_choices() 378 379 380 def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): 381 "Returns flattened choices with a default blank choice included." 382 first_choice = include_blank and blank_choice or [] 383 return first_choices + list(self.flatchoices) 384 379 385 def _get_val_from_obj(self, obj): 380 386 if obj: … … 409 415 410 416 def _get_flatchoices(self): 417 """Flattened version of choices tuple.""" 411 418 flat = [] 412 for choice, value in self. get_choices_default():419 for choice, value in self.choices: 413 420 if type(value) in (list, tuple): 414 421 flat.extend(value) … … 417 424 return flat 418 425 flatchoices = property(_get_flatchoices) 419 426 420 427 def save_form_data(self, instance, data): 421 428 setattr(instance, self.name, data) django/trunk/tests/modeltests/choices/models.py
r5876 r8102 37 37 >>> s.get_gender_display() 38 38 u'Female' 39 40 # If the value for the field doesn't correspond to a valid choice, 41 # the value itself is provided as a display value. 42 >>> a.gender = '' 43 >>> a.get_gender_display() 44 u'' 45 46 >>> a.gender = 'U' 47 >>> a.get_gender_display() 48 u'U' 49 39 50 """} django/trunk/tests/regressiontests/model_fields/models.py
r7331 r8102 12 12 a = models.ForeignKey(Foo, default=get_foo) 13 13 14 class Whiz(models.Model): 15 CHOICES = ( 16 ('Group 1', ( 17 (1,'First'), 18 (2,'Second'), 19 ) 20 ), 21 ('Group 2', ( 22 (3,'Third'), 23 (4,'Fourth'), 24 ) 25 ), 26 (0,'Other'), 27 ) 28 c = models.IntegerField(choices=CHOICES, null=True) 29 14 30 __test__ = {'API_TESTS':""" 15 31 # Create a couple of Places. … … 22 38 >>> b.save() 23 39 40 # Regression tests for #7913 41 # Check that get_choices and get_flatchoices interact with 42 # get_FIELD_display to return the expected values. 43 44 # Test a nested value 45 >>> w = Whiz(c=1) 46 >>> w.save() 47 >>> w.get_c_display() 48 u'First' 49 50 # Test a top level value 51 >>> w.c = 0 52 >>> w.save() 53 >>> w.get_c_display() 54 u'Other' 55 56 # Test an invalid data value 57 >>> w.c = 9 58 >>> w.save() 59 >>> w.get_c_display() 60 9 61 62 # Test a blank data value 63 >>> w.c = None 64 >>> w.save() 65 >>> print w.get_c_display() 66 None 67 68 # Test an empty data value 69 >>> w.c = '' 70 >>> w.save() 71 >>> w.get_c_display() 72 u'' 73 74 24 75 """}
