MultiWidget
A widget that is composed of multiple widgets.
Attributes
| Attribute | Type | Description |
|---|---|---|
| template_name | string = "django/forms/widgets/multiwidget.html" | The path to the HTML template used to render the multi-widget container. |
| use_fieldset | boolean = true | Determines whether the group of subwidgets should be wrapped in a fieldset element for accessibility and semantic grouping. |
| media | property | Media for a multiwidget is the combination of all media of the subwidgets. |
Constructor
Signature
def MultiWidget(
widgets: list or dict,
attrs: dict = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| widgets | list or dict | An iterable or dictionary of widget instances or widget classes to be composed. |
| attrs | dict = None | Optional 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
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| name | string | The base name of the form field |
| value | any | The value(s) to be assigned to the subwidgets, which will be decompressed if not already a list or tuple |
| attrs | dict | HTML attributes to be applied to the widget container and propagated to subwidgets |
Returns
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| id_ | string | The ID of the widget |
Returns
| Type | Description |
|---|---|
string | An 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of submitted file data |
| name | string | The base name of the multi-widget field |
Returns
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of submitted file data |
| name | string | The base name of the multi-widget field |
Returns
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| value | any | The combined value to be split into sub-values |
Returns
| Type | Description |
|---|---|
list | A 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
| Type | Description |
|---|---|
boolean | True if at least one subwidget requires a multipart form (e.g., for file uploads), otherwise False |