Ticket #2037: 2037_2.diff

File 2037_2.diff, 2.8 KB (added by Marc Fargas <telenieko@…>, 8 years ago)

ForeignKeys didn't work as expected

  • 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 or self.null) 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
     31class ChoiceableModel(models.Model):
     32    emptynotallowed = models.CharField(maxlength=1, choices=CHOICES)
     33    emptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True)
     34    defaultemptynotallowed = models.CharField(maxlength=1, choices=CHOICES, default=2)
     35    defaultemptyallowed = models.CharField(maxlength=1, choices=CHOICES, null=True, blank=True, default=2)
     36    nullrelation = models.ForeignKey(Person, null=True)
     37    relation = models.ForeignKey(Person, related_name='notnulrelation')
     38
    2639__test__ = {'API_TESTS':"""
    2740>>> a = Person(name='Adrian', gender='M')
    2841>>> a.save()
     
    3649'Male'
    3750>>> s.get_gender_display()
    3851'Female'
     52>>> ChoiceableModel._meta.fields[1].get_choices()
     53[(0, 'State A'), (1, 'State B')]
     54>>> ChoiceableModel._meta.fields[2].get_choices()
     55[('', '---------'), (0, 'State A'), (1, 'State B')]
     56>>> ChoiceableModel._meta.fields[3].get_choices()
     57[(0, 'State A'), (1, 'State B')]
     58>>> ChoiceableModel._meta.fields[4].get_choices()
     59[('', '---------'), (0, 'State A'), (1, 'State B')]
     60>>> ChoiceableModel._meta.fields[5].get_choices()
     61[('', '---------'), (1, 'Adrian'), (2, 'Sara')]
     62>>> ChoiceableModel._meta.fields[6].get_choices()
     63[(1, 'Adrian'), (2, 'Sara')]
    3964"""}
  • 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