Autocomplete-Light Frontend (dal_alight) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. _alight-tutorial: .. note:: **For demo links** to work, you need to run the :ref:`test project ` on localhost. Overview ======== ``dal_alight`` is a DAL autocomplete frontend built on native `Web Components `_. No jQuery or third-party JS library is required. Key differences from the Select2 frontend: - **No jQuery / Select2 required.** The component is a pure web component (````) with no external library dependency. - **The view returns HTML fragments** instead of JSON. Each result is a ``
`` element; the JS component reads those divs to build the dropdown. - Static files served are ``dal_alight/autocomplete-light.css``, ``dal_alight/autocomplete-light.js``, and ``dal_alight/dal-django.js``. Install ======= Add ``dal_alight`` to ``INSTALLED_APPS`` **before** ``django.contrib.admin``: .. code-block:: python INSTALLED_APPS = [ 'dal', 'dal_alight', # 'grappelli', 'django.contrib.admin', ... ] For Generic Foreign Key support also add ``dal_queryset_sequence``: .. code-block:: python INSTALLED_APPS = [ 'dal', 'dal_alight', 'dal_queryset_sequence', 'dal_alight_queryset_sequence', # bridges the two ... ] .. _alight-queryset-view: Create an autocomplete view =========================== - Example source: `test_project/alight_foreign_key `_ - Live demo: `/alight_foreign_key/test-autocomplete/?q=test `_ Use :py:class:`~dal_alight.views.AlightQuerySetView`: .. code-block:: python from dal import autocomplete from your_countries_app.models import Country class CountryAutocomplete(autocomplete.AlightQuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return Country.objects.none() qs = Country.objects.all() if self.q: qs = qs.filter(name__istartswith=self.q) return qs Register the view ================= .. code-block:: python from django.urls import path from your_countries_app.views import CountryAutocomplete urlpatterns = [ path( 'country-autocomplete/', CountryAutocomplete.as_view(), name='country-autocomplete', ), ] The simplest registration passes the model directly without a custom view class: .. code-block:: python from dal import autocomplete from your_countries_app.models import Country urlpatterns = [ path( 'country-autocomplete/', autocomplete.AlightQuerySetView.as_view(model=Country), name='country-autocomplete', ), ] .. danger:: As with all DAL views, the URL is public by default. Always check permissions in ``get_queryset()``. Use the view in a Form widget ============================= ForeignKey (single select) -------------------------- Use :py:class:`~dal_alight.widgets.ModelAlight` for a ``ForeignKey`` field: .. code-block:: python from dal import autocomplete from django import forms class PersonForm(forms.ModelForm): class Meta: model = Person fields = ('__all__',) widgets = { 'birth_country': autocomplete.ModelAlight(url='country-autocomplete') } ManyToManyField (multi select) ------------------------------ Use :py:class:`~dal_alight.widgets.ModelAlightMultiple` for a ``ManyToManyField``: .. code-block:: python widgets = { 'visited_countries': autocomplete.ModelAlightMultiple(url='country-autocomplete') } Initial values on edit forms ---------------------------- :py:class:`~dal_alight.widgets.ModelAlight` and :py:class:`~dal_alight.widgets.ModelAlightMultiple` inject the currently selected object(s) into the ``