Skip to main content

MultiWidget

A widget that is composed of multiple widgets.

Attributes

AttributeTypeDescription
template_namestring = "django/forms/widgets/multiwidget.html"The path to the HTML template used to render the multi-widget container.
use_fieldsetboolean = trueDetermines whether the group of subwidgets should be wrapped in a fieldset element for accessibility and semantic grouping.
mediapropertyMedia for a multiwidget is the combination of all media of the subwidgets.

Constructor

Signature

def MultiWidget(
widgets: list or dict,
attrs: dict = None
)

Parameters

NameTypeDescription
widgetslist or dictAn iterable or dictionary of widget instances or widget classes to be composed.
attrsdict = NoneOptional HTML attributes to be rendered on the outer widget container.

Methods


is_hidden()

@classmethod
def is_hidden() - > boolean

Determines if the multi-widget is hidden by checking if all of its constituent subwidgets are hidden.

Returns

TypeDescription
booleanTrue if every subwidget in the collection is hidden, otherwise False

get_context()

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

In addition to the values added by Widget.get_context(), this widget adds a list of subwidgets to the context as widget['subwidgets'].

Parameters

NameTypeDescription
namestringThe base name of the form field
valueanyThe value(s) to be assigned to the subwidgets, which will be decompressed if not already a list or tuple
attrsdictHTML attributes to be applied to the widget container and propagated to subwidgets

Returns

TypeDescription
dictA dictionary containing the template context, including the list of rendered subwidgets

id_for_label()

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

Returns an empty string because a multi-widget does not have a single unique ID for a label to reference.

Parameters

NameTypeDescription
id_stringThe ID of the widget

Returns

TypeDescription
stringAn empty string

value_from_datadict()

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

Extracts values for all subwidgets from the submitted data dictionary using their specific suffixed names.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data
filesdictThe dictionary of submitted file data
namestringThe base name of the multi-widget field

Returns

TypeDescription
listA list of values retrieved from the data dictionary for each subwidget

value_omitted_from_data()

@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: string
) - > boolean

Checks if the data for all subwidgets is missing from the submitted data and files.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data
filesdictThe dictionary of submitted file data
namestringThe base name of the multi-widget field

Returns

TypeDescription
booleanTrue if every subwidget's value is omitted from the data, otherwise False

decompress()

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

Return a list of decompressed values for the given compressed value. The given value can be assumed to be valid, but not necessarily non-empty.

Parameters

NameTypeDescription
valueanyThe combined value to be split into sub-values

Returns

TypeDescription
listA list of individual values corresponding to each subwidget

needs_multipart_form()

@classmethod
def needs_multipart_form() - > boolean

Determines if the form requires multipart encoding by checking if any of the subwidgets require it.

Returns

TypeDescription
booleanTrue if at least one subwidget requires a multipart form (e.g., for file uploads), otherwise False