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. RequiredNullBooleanField by wwu.housing 4 years, 1 month ago
  2. Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96 by chrj 4 years, 9 months ago
  3. Choices class by dc 4 years, 6 months ago
  4. Settings file pre by beresovskiy 1 year, 1 month ago
  5. AgreementField by chrisrbennett 4 years, 11 months ago

Comments

(Forgotten your password?)