CheckboxSelectMultiple
This class provides a form widget that renders a multiple-choice selection as a list of HTML checkbox inputs. It extends the radio selection functionality to allow for multiple active selections while disabling standard browser-side required validation to ensure flexible data entry. The widget uses specific templates for rendering the group and individual checkbox options.
Attributes
| Attribute | Type | Description |
|---|---|---|
| allow_multiple_selected | boolean = True | Boolean flag indicating that the widget supports selecting more than one option simultaneously. |
| input_type | string = checkbox | The HTML input type attribute used when rendering the widget's selection elements. |
| template_name | string = django/forms/widgets/checkbox_select.html | The path to the Django HTML template used to render the outer structure of the checkbox group. |
| option_template_name | string = django/forms/widgets/checkbox_option.html | The path to the Django HTML template used to render each individual checkbox option within the group. |
Methods
use_required_attribute()
@classmethod
def use_required_attribute(
initial: any
) - > boolean
Don't use the 'required' attribute because browser validation would require all checkboxes to be checked instead of at least one.
Parameters
| Name | Type | Description |
|---|---|---|
| initial | any | The initial value of the field used to determine if the attribute is necessary |
Returns
| Type | Description |
|---|---|
boolean | Always returns False to prevent incorrect browser-side validation of multiple checkboxes |
value_omitted_from_data()
@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: string
) - > boolean
HTML checkboxes don't appear in POST data if not checked, so it's never known if the value is actually omitted.
Parameters
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of submitted file data |
| name | string | The name of the widget used to look up values in the data dictionaries |
Returns
| Type | Description |
|---|---|
boolean | Always returns False because an empty submission is a valid state for multiple checkboxes |