Skip to main content

BooleanField

This class represents a form field for boolean input, typically rendered as a checkbox. It handles the normalization of various input types, such as specific strings or numeric values, into Python boolean objects. The class also manages validation logic to ensure required fields are checked and provides mechanisms to detect changes between initial and submitted data.

Attributes

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = CheckboxInputThe default widget class used to render this field in HTML forms, which defaults to a checkbox input.

Methods


to_python()

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

Return a Python boolean object.

Parameters

NameTypeDescription
valueanyThe raw input value from a form field or widget to be converted into a boolean.

Returns

TypeDescription
boolThe converted boolean value, handling string representations like 'false' or '0' as False.

validate()

@classmethod
def validate(
value: any
)

Validates that the input value satisfies the field requirements, specifically checking if a required field is empty.

Parameters

NameTypeDescription
valueanyThe python-converted value to check against validation rules.

has_changed()

@classmethod
def has_changed(
initial: any,
data: any
) - > bool

Determines if the field value has changed by comparing the initial value and the submitted data after normalizing both to booleans.

Parameters

NameTypeDescription
initialanyThe original value of the field before user modification.
dataanyThe new value submitted via the form data.

Returns

TypeDescription
boolTrue if the normalized boolean values differ and the field is not disabled, otherwise False.