Ticket #2037: 2037.diff

File 2037.diff, 2.5 KB (added by Marc Fargas <telenieko@…>, 17 years ago)

Patching the behaviour of Field.get_choices() + tests

  • django/db/models/fields/__init__.py

    === modified file 'django/db/models/fields/__init__.py'
     
    294294
    295295    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
    296296        "Returns a list of tuples used as SelectField choices for this field."
    297         first_choice = include_blank and blank_choice or []
     297        first_choice = include_blank and self.blank and blank_choice or []
    298298        if self.choices:
    299299            return first_choice + list(self.choices)
    300300        rel_model = self.rel.to
  • tests/modeltests/choices/models.py

    === modified file 'tests/modeltests/choices/models.py'
     
    2323    def __str__(self):
    2424        return self.name
    2525
     26CHOICES = (
     27    (0, 'State A'),
     28    (1, 'State B'),
     29)
     30
     31
     32class ChoiceableModel(models.Model):
     33    emptynotallowed = models.CharField(maxlength=1, choices=CHOICES)
     34    emptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True)
     35    defaultemptynotallowed = models.CharField(maxlength=1, choices=CHOICES, default=2)
     36    defaultemptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True, default=2)
     37
    2638__test__ = {'API_TESTS':"""
    2739>>> a = Person(name='Adrian', gender='M')
    2840>>> a.save()
     
    3648'Male'
    3749>>> s.get_gender_display()
    3850'Female'
     51>>> ChoiceableModel._meta.fields[1].get_choices()
     52[(0, 'State A'), (1, 'State B')]
     53>>> ChoiceableModel._meta.fields[2].get_choices()
     54[('', '---------'), (0, 'State A'), (1, 'State B')]
     55>>> ChoiceableModel._meta.fields[3].get_choices()
     56[(0, 'State A'), (1, 'State B')]
     57>>> ChoiceableModel._meta.fields[4].get_choices()
     58[('', '---------'), (0, 'State A'), (1, 'State B')]
    3959"""}
  • tests/modeltests/manipulators/models.py

    === modified file 'tests/modeltests/manipulators/models.py'
     
    5454
    5555# Attempt to create an Album with an invalid musician.
    5656>>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']}))
    57 {'musician': ["Select a valid choice; 'foo' is not in ['', '1']."]}
     57{'musician': ["Select a valid choice; 'foo' is not in ['1']."]}
    5858
    5959# Attempt to create an Album with an invalid release_date.
    6060>>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'}))
Back to Top