AutocompleteMixin
Select widget mixin that loads options from AutocompleteJsonView via AJAX.
Renders the necessary data attributes for select2 and adds the static form media.
Attributes
| Attribute | Type | Description |
|---|---|---|
| url_name | string = %s:autocomplete | A string template used to resolve the AJAX endpoint URL for the autocomplete search results, formatted with the admin site name. |
Constructor
Signature
def AutocompleteMixin(
field: [Field](../../../forms/fields/field.md?sid=django_forms_fields_field),
admin_site: [AdminSite](../sites/adminsite.md?sid=django_contrib_admin_sites_adminsite),
attrs: dict = None,
choices: tuple = (),
using: str = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| field | [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) | The model field associated with the autocomplete widget. |
| admin_site | [AdminSite](../sites/adminsite.md?sid=django_contrib_admin_sites_adminsite) | The admin site instance where the widget is being used. |
| attrs | dict = None | Optional dictionary of HTML attributes to be added to the widget. |
| choices | tuple = () | A sequence of choices to be used in the widget. |
| using | str = None | The database alias to use for queries. |
Signature
def AutocompleteMixin(
field: django.db.models.Field,
admin_site: django.contrib.admin.sites.AdminSite,
attrs: dict = None,
choices: tuple = (),
using: str = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| field | django.db.models.Field | The model field associated with this widget. |
| admin_site | django.contrib.admin.sites.AdminSite | The admin site instance used to resolve the autocomplete URL. |
| attrs | dict = None | A dictionary of HTML attributes to be added to the rendered widget. |
| choices | tuple = () | The initial collection of choices for the select widget. |
| using | str = None | The database alias to use for querying model instances. |
Methods
get_url()
@classmethod
def get_url() - > str
Constructs the absolute URL for the AutocompleteJsonView based on the current admin site's namespace.
Returns
| Type | Description |
|---|---|
str | The resolved URL path where the widget will send AJAX search requests. |
build_attrs()
@classmethod
def build_attrs(
base_attrs: dict,
extra_attrs: dict = None
) - > dict
Set select2's AJAX attributes. Attributes can be set using the html5 data attribute. Nested attributes require a double dash as per https://select2.org/configuration/data-attributes#nested-subkey-options
Parameters
| Name | Type | Description |
|---|---|---|
| base_attrs | dict | The widget's base HTML attributes. |
| extra_attrs | dict = None | Additional attributes to merge into the final attribute set. |
Returns
| Type | Description |
|---|---|
dict | A dictionary of HTML attributes including data-ajax and data-model properties for Select2 initialization. |
optgroups()
@classmethod
def optgroups(
name: str,
value: list,
attr: dict = None
) - > list
Return selected options based on the ModelChoiceIterator.
Parameters
| Name | Type | Description |
|---|---|---|
| name | str | The name of the form field. |
| value | list | The list of currently selected primary keys. |
| attr | dict = None | Optional HTML attributes for the optgroup. |
Returns
| Type | Description |
|---|---|
list | A list of option groups containing the currently selected model instances as option dictionaries. |
media()
@classmethod
def media() - > django.forms.Media
Assembles the required JavaScript and CSS assets for Select2 and the Django admin autocomplete integration.
Returns
| Type | Description |
|---|---|
django.forms.Media | A Media object containing paths to jQuery, Select2, and local autocomplete initialization scripts. |