Django

Code

Changeset 8102

Show
Ignore:
Timestamp:
07/27/08 02:22:39 (4 months ago)
Author:
russellm
Message:

Fixed #7913 -- Corrected backwards incompatible parts of [7977] when optgroup handling was added to field choices (Ticket #4412). Thanks to Michael Elsdorfer (miracle2k) for the report and patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r8050 r8102  
    289289            field_objs = [oldforms.SelectField] 
    290290 
    291             params['choices'] = self.flatchoices 
     291            params['choices'] = self.get_flatchoices() 
    292292        else: 
    293293            field_objs = self.get_manipulator_field_objs() 
     
    363363 
    364364    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.""" 
    366367        first_choice = include_blank and blank_choice or [] 
    367368        if self.choices: 
     
    377378        return self.get_choices() 
    378379 
     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 
    379385    def _get_val_from_obj(self, obj): 
    380386        if obj: 
     
    409415 
    410416    def _get_flatchoices(self): 
     417        """Flattened version of choices tuple.""" 
    411418        flat = [] 
    412         for choice, value in self.get_choices_default()
     419        for choice, value in self.choices
    413420            if type(value) in (list, tuple): 
    414421                flat.extend(value) 
     
    417424        return flat 
    418425    flatchoices = property(_get_flatchoices) 
    419      
     426 
    420427    def save_form_data(self, instance, data): 
    421428        setattr(instance, self.name, data) 
  • django/trunk/tests/modeltests/choices/models.py

    r5876 r8102  
    3737>>> s.get_gender_display() 
    3838u'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() 
     44u'' 
     45 
     46>>> a.gender = 'U' 
     47>>> a.get_gender_display() 
     48u'U' 
     49 
    3950"""} 
  • django/trunk/tests/regressiontests/model_fields/models.py

    r7331 r8102  
    1212    a = models.ForeignKey(Foo, default=get_foo) 
    1313 
     14class 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     
    1430__test__ = {'API_TESTS':""" 
    1531# Create a couple of Places. 
     
    2238>>> b.save() 
    2339 
     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() 
     48u'First' 
     49 
     50# Test a top level value 
     51>>> w.c = 0 
     52>>> w.save() 
     53>>> w.get_c_display() 
     54u'Other' 
     55 
     56# Test an invalid data value 
     57>>> w.c = 9 
     58>>> w.save() 
     59>>> w.get_c_display() 
     609 
     61 
     62# Test a blank data value 
     63>>> w.c = None 
     64>>> w.save() 
     65>>> print w.get_c_display() 
     66None 
     67 
     68# Test an empty data value 
     69>>> w.c = '' 
     70>>> w.save() 
     71>>> w.get_c_display() 
     72u'' 
     73 
     74 
    2475"""}