Skip to main content

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

AttributeTypeDescription
url_namestring = %s:autocompleteA 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

NameTypeDescription
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.
attrsdict = NoneOptional dictionary of HTML attributes to be added to the widget.
choicestuple = ()A sequence of choices to be used in the widget.
usingstr = NoneThe 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

NameTypeDescription
fielddjango.db.models.FieldThe model field associated with this widget.
admin_sitedjango.contrib.admin.sites.AdminSiteThe admin site instance used to resolve the autocomplete URL.
attrsdict = NoneA dictionary of HTML attributes to be added to the rendered widget.
choicestuple = ()The initial collection of choices for the select widget.
usingstr = NoneThe 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

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

NameTypeDescription
base_attrsdictThe widget's base HTML attributes.
extra_attrsdict = NoneAdditional attributes to merge into the final attribute set.

Returns

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

NameTypeDescription
namestrThe name of the form field.
valuelistThe list of currently selected primary keys.
attrdict = NoneOptional HTML attributes for the optgroup.

Returns

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

TypeDescription
django.forms.MediaA Media object containing paths to jQuery, Select2, and local autocomplete initialization scripts.