BooleanField
class
BooleanField
A field that represents a boolean value.
BooleanField(
*,
required: bool = False,
default: Any = None,
messages: dict[str, str] | None = None
)
Bases: Field
| Argument | Description |
|---|---|
required |
Whether the field value must be |
default |
Default value for the field. Can be a static value or a callable.
Defaults to |
messages |
Dictionary of error codes to custom error message templates. These override the default error messages for this specific field. Example: {"required": "This field cannot be empty"}. |
Boolean fields are treated specially because of how browsers handle checkboxes:
- If not checked: the browser doesn't send the field at all.
- If checked: It sends the "value" attribute, but this is optional, so it could send an empty string instead.
For these reasons:
- A missing field (a
Nonevalue) will becomeFalse. - A string value in the
FALSE_VALUEStuple (case-insensitive) will becomeFalse. - Any other value, including an empty string, will become
True.