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 4 years, 4 months ago
  2. Template filter to markup form fields with optional args by fleggs 2 years, 4 months ago
  3. Allow filtering and ordering by counts of related query results by exogen 7 years ago
  4. FieldLevelPermissionsAdmin by buriy 6 years, 7 months ago
  5. Making the HTTP object available globally across the app by sleepycal 3 years, 2 months ago

Comments

(Forgotten your password?)