Skip to main content

ChoiceWidget

This class provides a base implementation for widgets that allow users to select from a predefined set of choices. It manages the generation of option groups and individual options, supporting both single and multiple selections through configurable attributes. The class also handles the normalization of choice data and the mapping of values to their corresponding HTML attributes.

Attributes

AttributeTypeDescription
allow_multiple_selectedboolean = falseBoolean flag indicating if the widget supports selecting more than one option simultaneously.
input_typestring = nullThe HTML input type attribute used when rendering the widget's options.
template_namestring = nullThe path to the template used to render the main widget structure.
option_template_namestring = nullThe path to the template used to render individual choice options within the widget.
add_id_indexboolean = trueDetermines whether an incrementing index should be appended to the HTML ID of each option.
checked_attributedict = {"checked": true}A dictionary containing the HTML attribute name and value to apply when an option is selected.
option_inherits_attrsboolean = trueSpecifies if individual options should inherit the base attributes defined on the widget instance.

Constructor

Signature

def ChoiceWidget(
attrs: dict = None,
choices: iterable = ()
) - > null

Parameters

NameTypeDescription
attrsdict = NoneA dictionary of HTML attributes to be added to the rendered widget.
choicesiterable = ()An iterable of choices to be used by the widget.

Methods


subwidgets()

@classmethod
def subwidgets(
name: string,
value: any,
attrs: dict
) - > generator

Yield all "subwidgets" of this widget. Used to enable iterating options from a BoundField for choice widgets.

Parameters

NameTypeDescription
namestringThe name attribute of the HTML input element.
valueanyThe current value(s) of the widget used to determine which options are selected.
attrsdictAdditional HTML attributes to be applied to the subwidgets.

Returns

TypeDescription
generatorA generator yielding individual option dictionaries for the widget.

options()

@classmethod
def options(
name: string,
value: any,
attrs: dict
) - > generator

Yield a flat list of options for this widget.

Parameters

NameTypeDescription
namestringThe name attribute of the HTML input element.
valueanyThe current value(s) of the widget.
attrsdictAdditional HTML attributes for the options.

Returns

TypeDescription
generatorA generator yielding flattened option dictionaries from all optgroups.

optgroups()

@classmethod
def optgroups(
name: string,
value: any,
attrs: dict
) - > list

Return a list of optgroups for this widget.

Parameters

NameTypeDescription
namestringThe name attribute of the HTML input element.
valueanyThe current value(s) of the widget used to mark options as selected.
attrsdictAdditional HTML attributes for the optgroup elements.

Returns

TypeDescription
listA list of tuples containing group names, subgroups of options, and group indices.

create_option()

@classmethod
def create_option(
name: string,
value: any,
label: string,
selected: boolean,
index: integer,
subindex: integer,
attrs: dict
) - > dict

Builds a dictionary representing a single choice option, including its attributes, label, and selection state.

Parameters

NameTypeDescription
namestringThe name attribute of the input.
valueanyThe value of the specific option.
labelstringThe human-readable text displayed for the option.
selectedbooleanWhether this option should be marked as selected or checked.
indexintegerThe positional index of the option within the choice list.
subindexintegerThe index of the option within a nested group, if applicable.
attrsdictBase HTML attributes to merge with option-specific attributes.

Returns

TypeDescription
dictA dictionary containing the configuration and metadata for rendering a single option template.

get_context()

@classmethod
def get_context(
name: string,
value: any,
attrs: dict
) - > dict

Constructs the template context for rendering the widget, including the calculated optgroups.

Parameters

NameTypeDescription
namestringThe name of the form field.
valueanyThe current value of the field.
attrsdictHTML attributes to be included in the context.

Returns

TypeDescription
dictA dictionary containing the widget state and optgroups for template rendering.

id_for_label()

@classmethod
def id_for_label(
id_: string,
index: string
) - > string

Use an incremented id for each option where the main widget references the zero index.

Parameters

NameTypeDescription
id_stringThe base ID of the widget.
indexstringThe index suffix to append to the base ID.

Returns

TypeDescription
stringThe generated HTML ID string for a specific option.

value_from_datadict()

@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: string
) - > any

Extracts the widget's value from a submission dictionary, handling both single and multiple selection logic.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data (e.g., request.POST).
filesdictThe dictionary of submitted file data.
namestringThe name of the field to look up in the data.

Returns

TypeDescription
anyThe submitted value or list of values associated with the widget name.

format_value()

@classmethod
def format_value(
value: any
) - > list

Return selected values as a list.

Parameters

NameTypeDescription
valueanyThe raw value to be formatted for the widget.

Returns

TypeDescription
listA list of string-formatted values representing the current selection.

choices()

@classmethod
def choices() - > list

Gets or sets the choices for the widget, ensuring they are normalized into a standard format upon assignment.

Returns

TypeDescription
listThe normalized list of choices available for the widget.