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
| Attribute | Type | Description |
|---|---|---|
| widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = CheckboxInput | The 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
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value from a form field or widget to be converted into a boolean. |
Returns
| Type | Description |
|---|---|
bool | The 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
| Name | Type | Description |
|---|---|---|
| value | any | The 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
| Name | Type | Description |
|---|---|---|
| initial | any | The original value of the field before user modification. |
| data | any | The new value submitted via the form data. |
Returns
| Type | Description |
|---|---|
bool | True if the normalized boolean values differ and the field is not disabled, otherwise False. |