SplitArrayWidget
This class provides a wrapper for a base widget to handle array-like data by rendering a fixed number of sub-widgets. It manages the distribution of values across multiple instances of the underlying widget and handles data retrieval from form submissions using indexed keys. The class also ensures that attributes, media, and multipart form requirements are correctly delegated to the internal widget type.
Attributes
| Attribute | Type | Description |
|---|---|---|
| template_name | string = "postgres/widgets/split_array.html" | The path to the HTML template used to render the split array widget components. |
Constructor
Signature
def SplitArrayWidget(
widget: [Widget](../../../../forms/widgets/widget.md?sid=django_forms_widgets_widget),
size: int,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| widget | [Widget](../../../../forms/widgets/widget.md?sid=django_forms_widgets_widget) | The widget class or instance to be used for each element in the array. |
| size | int | The number of elements to be displayed in the array. |
| **kwargs | dict | Additional keyword arguments passed to the parent Widget class. |
Methods
is_hidden()
@classmethod
def is_hidden() - > boolean
Indicates whether the underlying sub-widget is of a hidden type.
Returns
| Type | Description |
|---|---|
boolean | True if the wrapped widget is hidden, False otherwise |
value_from_datadict()
@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: string
) - > list
Extracts values for each array element from the submitted data dictionary using indexed keys.
Parameters
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of uploaded files |
| name | string | The base name of the form field |
Returns
| Type | Description |
|---|---|
list | A list of values retrieved from the data dictionary for each index in the array |
value_omitted_from_data()
@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: string
) - > boolean
Determines if data for all expected array indices is missing from the submission.
Parameters
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data |
| files | dict | The dictionary of uploaded files |
| name | string | The base name of the form field |
Returns
| Type | Description |
|---|---|
boolean | True if every indexed sub-widget value is absent from the data, False otherwise |
id_for_label()
@classmethod
def id_for_label(
id_: string
) - > string
Returns the HTML ID attribute for the first element in the array to associate with a label.
Parameters
| Name | Type | Description |
|---|---|---|
| id_ | string | The base ID of the widget |
Returns
| Type | Description |
|---|---|
string | The ID of the first sub-widget, typically suffixed with '_0' |
get_context()
@classmethod
def get_context(
name: string,
value: list,
attrs: dict
) - > dict
Constructs the template context, populating it with a list of sub-widgets for each array index.
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | The name of the field |
| value | list | The list of values to populate the sub-widgets |
| attrs | dict | Optional HTML attributes to apply to the widget |
Returns
| Type | Description |
|---|---|
dict | A dictionary containing the widget state and a list of sub-widget contexts for rendering |
media()
@classmethod
def media() - > [Media](../../../../forms/widgets/media.md?sid=django_forms_widgets_media)
Retrieves the CSS and JavaScript assets required by the underlying sub-widget.
Returns
| Type | Description |
|---|---|
[Media](../../../../forms/widgets/media.md?sid=django_forms_widgets_media) | A Django Media object containing the required static file paths |
needs_multipart_form()
@classmethod
def needs_multipart_form() - > boolean
Checks if the underlying sub-widget requires a multipart/form-data encoding for file uploads.
Returns
| Type | Description |
|---|---|
boolean | True if the sub-widget handles file uploads, False otherwise |