ManyToManyRawIdWidget
A Widget for displaying ManyToMany ids in the "raw_id" interface rather than in a < select multiple > box.
Attributes
| Attribute | Type | Description |
|---|---|---|
| template_name | string = "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
| Name | Type | Description |
|---|---|---|
| name | str | The name of the form field. |
| value | any | The current value of the field to be rendered. |
| attrs | dict | Additional HTML attributes to be added to the widget's root element. |
Returns
| Type | Description |
|---|---|
dict | A 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
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| value | any | The primary key value or list of values for which to retrieve a label and URL. |
Returns
| Type | Description |
|---|---|
tuple | A 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
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted form data (typically request.POST). |
| files | dict | The dictionary of submitted file data (typically request.FILES). |
| name | str | The name of the form field to look up in the data dictionary. |
Returns
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| value | list | The list of primary keys or related objects to be formatted for the widget input. |
Returns
| Type | Description |
|---|---|
str | A comma-separated string of IDs, or an empty string if the value is null or empty. |