Skip to main content

ManyToManyRawIdWidget

A Widget for displaying ManyToMany ids in the "raw_id" interface rather than in a < select multiple > box.

Attributes

AttributeTypeDescription
template_namestring = "admin/widgets/many_to_many_raw_id.html"The path to the HTML template used to render the ManyToMany raw ID widget.

Constructor

Signature

def ManyToManyRawIdWidget()

Methods


get_context()

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

Returns the template context for rendering the widget, ensuring the 'vManyToManyRawIdAdminField' CSS class is applied if the related model is registered with the admin site.

Parameters

NameTypeDescription
namestrThe name of the form field.
valueanyThe current value of the field to be rendered.
attrsdictAdditional HTML attributes to be added to the widget's root element.

Returns

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

url_parameters()

@classmethod
def url_parameters() - > dict

Fetches the base URL parameters required for the related lookup popup window.

Returns

TypeDescription
dictA dictionary of query parameters used to filter or configure the related object selection interface.

label_and_url_for_value()

@classmethod
def label_and_url_for_value(
value: any
) - > tuple

Returns empty strings for the label and URL, as ManyToMany raw ID widgets typically do not display a single descriptive label for multiple selected IDs.

Parameters

NameTypeDescription
valueanyThe primary key value or list of values for which to retrieve a label and URL.

Returns

TypeDescription
tupleA tuple containing two empty strings representing the label and the detail URL.

value_from_datadict()

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

Extracts the field value from the submitted form data and splits the comma-separated string of IDs into a list.

Parameters

NameTypeDescription
datadictThe dictionary of submitted form data (typically request.POST).
filesdictThe dictionary of submitted file data (typically request.FILES).
namestrThe name of the form field to look up in the data dictionary.

Returns

TypeDescription
listA list of individual primary key strings extracted from the submitted data, or None if no data exists.

format_value()

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

Converts a list of primary keys into a single comma-separated string for display in the HTML input field.

Parameters

NameTypeDescription
valuelistThe list of primary keys or related objects to be formatted for the widget input.

Returns

TypeDescription
strA comma-separated string of IDs, or an empty string if the value is null or empty.