BooleanField that treats '0' as unchecked

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def check_test(v):
    return not (
        v is False
        or v is None
        or v == ''
        or v == '0'
    )

class CheckBoxInput(forms.CheckboxInput):
    def __init__(self, attrs=None, check_test=None):
        if check_test is None:
            check_test = check_test
        return super(CheckBoxInput, self).__init__(attrs, check_test)

class BooleanField(forms.BooleanField):
    '''
    BooleanField that treats '0' as unchecked.

    '''
    widget = CheckBoxInput

More like this

  1. Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96 by chrj 3 years, 9 months ago
  2. RequiredNullBooleanField by wwu.housing 3 years, 1 month ago
  3. AgreementField by chrisrbennett 3 years, 11 months ago
  4. Online boolean switch in the admin list by sasha 4 years, 8 months ago
  5. CheckedField by oggy 3 years, 8 months ago

Comments

(Forgotten your password?)