Skip to main content

NullBooleanField

A field whose valid values are None, True, and False. Clean invalid values to None.

Attributes

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = NullBooleanSelectThe default widget class used to render this field in HTML forms, specifically designed to handle the three-state selection of None, True, and False.

Methods


to_python()

@classmethod
def to_python(
value: any
) - > boolean

Explicitly check for the string 'True' and 'False', which is what a hidden field will submit for True and False, for 'true' and 'false', which are likely to be returned by JavaScript serializations of forms, and for '1' and '0', which is what a RadioField will submit. Unlike the Booleanfield, this field must check for True because it doesn't use the bool() function.

Parameters

NameTypeDescription
valueanyThe raw input value from a form submission, JavaScript serialization, or hidden field to be converted into a Python boolean or None.

Returns

TypeDescription
booleanReturns True or False if the input matches a boolean-like value, otherwise returns None.

validate()

@classmethod
def validate(
value: any
) - > null

Performs no validation on the provided value, allowing any input that has been processed by to_python to pass.

Parameters

NameTypeDescription
valueanyThe Python-converted value to be validated against field constraints.

Returns

TypeDescription
nullAlways returns None.