Django

Code

Ticket #7913: 7913.diff

File 7913.diff, 2.5 kB (added by miracle2k, 6 months ago)
  • a/django/db/models/fields/__init__.py

    old new  
    288288        if self.choices: 
    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() 
    294294        return (field_objs, params) 
     
    362362        return val 
    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: 
    368369            return first_choice + list(self.choices) 
     
    376377    def get_choices_default(self): 
    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: 
    381387            return getattr(obj, self.attname) 
     
    408414    choices = property(_get_choices) 
    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) 
    415422            else: 
    416423                flat.append((choice,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) 
    422429 
  • a/tests/modeltests/choices/models.py

    old new  
    3636u'Male' 
    3737>>> s.get_gender_display() 
    3838u'Female' 
     39 
     40# 7913 
     41>>> a.gender = '' 
     42>>> a.get_gender_display() 
     43u'' 
    3944"""}