Skip to main content

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

AttributeTypeDescription
template_namestring = "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

NameTypeDescription
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.
sizeintThe number of elements to be displayed in the array.
**kwargsdictAdditional 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

TypeDescription
booleanTrue 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

NameTypeDescription
datadictThe dictionary of submitted form data
filesdictThe dictionary of uploaded files
namestringThe base name of the form field

Returns

TypeDescription
listA 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

NameTypeDescription
datadictThe dictionary of submitted form data
filesdictThe dictionary of uploaded files
namestringThe base name of the form field

Returns

TypeDescription
booleanTrue 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

NameTypeDescription
id_stringThe base ID of the widget

Returns

TypeDescription
stringThe 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

NameTypeDescription
namestringThe name of the field
valuelistThe list of values to populate the sub-widgets
attrsdictOptional HTML attributes to apply to the widget

Returns

TypeDescription
dictA 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

TypeDescription
[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

TypeDescription
booleanTrue if the sub-widget handles file uploads, False otherwise