RequiredNullBooleanField

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# fields.py
from django import forms

class RequiredNullBooleanField(forms.NullBooleanField):
    def clean(self, value):
        value = super(RequiredNullBooleanField, self).clean(value)
        if value is None:
            raise forms.ValidationError("This field is required.")
        return value


# forms.py
from django import forms
from project.fields import RequiredNullBooleanField

class MyForm(forms.Form):
    question = RequiredNullBooleanField(label="Have you ever travelled in an airplane?", 
                                        widget=forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]))

More like this

  1. TrueNoneField by diverman 3 years, 6 months ago
  2. BooleanField that treats '0' as unchecked by johnboxall 1 year, 6 months ago
  3. Django Template "include_raw" tag by wwu.housing 3 years, 10 months ago
  4. Improved Pickled Object Field by taavi223 3 years, 10 months ago
  5. Making the HTTP object available globally across the app by sleepycal 2 years, 4 months ago

Comments

(Forgotten your password?)