Ticket #7913: 7913.diff

File 7913.diff, 2.5 KB (added by miracle2k, 7 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 439633c..dd5117f 100644
    a b class Field(object): 
    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)
    class Field(object): 
    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)
    class Field(object): 
    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)
    class Field(object): 
    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
  • tests/modeltests/choices/models.py

    diff --git a/tests/modeltests/choices/models.py b/tests/modeltests/choices/models.py
    index 550e655..0521fd8 100644
    a b __test__ = {'API_TESTS':""" 
    3636u'Male'
    3737>>> s.get_gender_display()
    3838u'Female'
     39
     40# 7913
     41>>> a.gender = ''
     42>>> a.get_gender_display()
     43u''
    3944"""}
Back to Top