Ticket #7753: 7753.nullboolean.diff

File 7753.nullboolean.diff, 2.0 KB (added by Julien Phalip, 16 years ago)

Same patch with, hopefully, stronger tests

  • django/django/forms/fields.py

     
    594594    widget = NullBooleanSelect
    595595
    596596    def clean(self, value):
    597         return {True: True, False: False}.get(value, None)
     597        """
     598        Explicitly checks for the string 'True' and 'False', which is what a
     599        hidden field will submit for True and False. Unlike the
     600        Booleanfield we also need to check for True, because we are not using
     601        the bool() function
     602        """
     603        if value in (True, 'True'):
     604            return True
     605        elif value in (False, 'False'):
     606            return False
     607        else:
     608            return None
    598609
    599610class ChoiceField(Field):
    600611    widget = Select
  • django/tests/regressiontests/forms/fields.py

     
    10911091>>> f.clean('3')
    10921092>>> f.clean('hello')
    10931093
     1094# Make sure that the internal value is preserved if using HiddenInput (#7753)
     1095>>> class HiddenNullBooleanForm(Form):
     1096...     hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True)
     1097...     hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False)
     1098>>> f = HiddenNullBooleanForm()
     1099>>> print f
     1100<input type="hidden" name="hidden_nullbool1" value="True" id="id_hidden_nullbool1" /><input type="hidden" name="hidden_nullbool2" value="False" id="id_hidden_nullbool2" />
     1101>>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' })
     1102>>> f.full_clean()
     1103>>> f.cleaned_data['hidden_nullbool1']
     1104True
     1105>>> f.cleaned_data['hidden_nullbool2']
     1106False
     1107
    10941108# MultipleChoiceField #########################################################
    10951109
    10961110>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])
Back to Top