from django.forms import NullBooleanField, TypedChoiceField class StrictBooleanField(TypedChoiceField): def __init__(self, *args, coerce=lambda val: bool(val), empty_value='', **kwargs): super().__init__(*args, coerce=coerce, empty_value=empty_value, **kwargs) def to_python(self, value): """ Explicitly validate empty / missing values, otherwise normal conversion """ value = NullBooleanField.to_python(self, value) if value is None: raise ValidationError(_('Invalid Boolean value %(value)s'), params={'value': value}, code='invalid') return value