DEV Community

Arjun Adhikari
Arjun Adhikari

Posted on • Updated on

Implement Autocomplete in Django

Hello pals,
In this blog, I am going to guide you to implement autocomplete in Django admin interface. I chose admin interface because it provides CRUD ( CREATE-READ-UPDATE-DELETE ) view for defined models out of the box.

For implementing auto completion feature, I am going to use django-ajax-selects currently maintained by crucialfelix.


Let's start by creating a django project.
First of all, I'll create a virtual environment .venv. You can create virtual environment just by python -m venv .venv.
Let's activate the virtual environment .venv\Scripts\activate as I am currently on Windows environment.

After that, install django and django-ajax-selects using pip.

pip install django django-ajax-selects
Enter fullscreen mode Exit fullscreen mode

Following installing libraries, let's create django project named autocomplete_demo_project.

django-admin startproject autocomplete_demo_project
Enter fullscreen mode Exit fullscreen mode

Subsequently, Let's create a django app named blog .

python startapp blog
Enter fullscreen mode Exit fullscreen mode

And register the app 'blog' and 'ajax_select' in INSTALLED_APPS inside

# autocomplete_demo_project/
    'ajax_select', # new apps
Enter fullscreen mode Exit fullscreen mode

Thereafter, let's define URLConfs for ajax_select so it may load the necessary javascripts, stylesheets for autocompletion.

# autocomplete_demo_project/
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from ajax_select import urls as ajax_select_urls

urlpatterns = [
    path('ajax_select/', include(ajax_select_urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Enter fullscreen mode Exit fullscreen mode

Models and Lookups

We have to start through defining models which will later be used for observing auto completion in admin panel.
Without adding any complexity, I'll just start with a single field name in Blog model.

# blog/
from django.db import models
from django.utils.translation import gettext_lazy as _

class Blog(models.Model):
    name = models.CharField(_("Blog"), max_length=50)

def  __str__(self):
Enter fullscreen mode Exit fullscreen mode

Let's also create a lookup for the field name which fetches the most suitable result by matching the query entered while entering a record in admin panel. For doing so, let's create LookupChannel for our field name (NameLookup) in inside blog app directory.

# blog/
from ajax_select import register, LookupChannel

from .models import Blog

class  NameLookup(LookupChannel):
    model = Blog

    def  get_query(self, q, request):
        return  self.model.objects.filter(name__icontains=q).order_by('name')[:50]

    def  format_item_display(self, item):
        return  u"<span class='blog_name'>%s</span>"  %
Enter fullscreen mode Exit fullscreen mode

Here, we've observed two methods inside our NameLookup class. Let's dig into both of them:

  1. get_query method takes q parameter as query which will be used to filter the record in database. Here, we've used the name__icontains approach which will filter the matched records in DB with sub string query q and return the filtered records.
    We may also proceed with name__startswith approach which only tries to match the starting string with the records in DB with sub string query q and return the filtered records. But, let's just stick into one approach ( name__icontains approach) to implement the auto completion.

  2. format_item_display methods takes each filtered records and (data) which is the need in our case with proper styling. We can override the display properties to make it look better and match with our preferences.

Also notice that we've registered a lookup name names with register decorator which will later be used with form to match with required form field i.e. The form field with the property names will match with the above lookup.

ModelAdmin and Forms

Ensuing step will be to create Django forms which will be provided as property to our ModelAdmin which we'll later create in .

# blog/
from ajax_select.fields import AutoCompleteField
from django import forms

from .models import Blog

class  BlogForm(forms.ModelForm):

    name = AutoCompleteField('names')
    class  Meta:
        model = Blog
        fields = [
Enter fullscreen mode Exit fullscreen mode

It's worth noticing that the field name is an AutoCompleteField with the property names which is a lookup name we've registered in NameLookup class inside blog/ It's how we interlink forms and lookups using django-ajax-selects.

Following this, let's create ModelAdmin inside

# blog/
from ajax_select.admin import AjaxSelectAdmin
from django.contrib import admin

from .forms import BlogForm
from .models import Blog

class  BlogAdmin(AjaxSelectAdmin):

    form = BlogForm
Enter fullscreen mode Exit fullscreen mode

What we've done here is overriding the default form property provided by Django Admin and get it replaced by our BlogForm form object.

Also note that, the use of BlogForm isn't limited to ModelAdmin for auto completion. It can also be used in used defined templates by passing form inside context parameter from
For doing so,

{{ }}
{{ form }}
Enter fullscreen mode Exit fullscreen mode

We need to load ((}} to load the necessary JS and CSS files defined by ajax_selects app.


After defining lookups, models, forms and modeladmin, now we can make migrations and run the migration files.
python makemigrations blog
Post hoc, let's run those migration files.
python migrate
Then, create a superuser for accessing admin interface,
python createsuperuser

Then, log into to access admin interface and navigate to Blog model.

Try adding some records and you'll notice auto completion making work easy for you.


I've added some records in my models.
Added Records

Let's try to add another record to observe auto-completion.
Autcompletion Demo
Notice that, when I am typing A, the records with the letter A in their name appeared as dropdown so user may autocomplete the record.

Thank you for reading.

Top comments (0)