Install & Compatibility
Where this runs
tested against v3.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.595s · 67.9MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 3.2s · import 0.498s · 68MB
58MB installed
● package 58MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
LookupChannel
✓ from ajax_select import LookupChannel
register
✓ from ajax_select import register
make_ajax_form
✓ from ajax_select import make_ajax_form
AjaxSelectAdmin
✓ from ajax_select.admin import AjaxSelectAdmin
AutoCompleteSelect
✓ from ajax_select import AutoCompleteSelect
AutoCompleteSelectMultiple
✓ from ajax_select import AutoCompleteSelectMultiple
This quickstart demonstrates how to integrate `django-ajax-selects` into your Django project's admin interface. It includes defining a simple model, creating a `LookupChannel` for an Author model, configuring `settings.py` (by adding `ajax_select` to `INSTALLED_APPS`), configuring `urls.py` (by including `ajax_select.urls`), and finally using `make_ajax_form` with `AjaxSelectAdmin` to enable autocomplete for the ForeignKey field (`author`) in the Django admin. The `settings.configure()` block is for standalone demonstration purposes; in a real project, you would modify your existing configuration files.
import os
import django
from django.conf import settings
from django.urls import path, include
from django.db import models
from django.contrib import admin
# NOTE: In a real Django project, you would typically integrate these pieces
# into your existing settings.py, urls.py, models.py, lookup_channels.py, and admin.py files.
# The settings.configure() block below is for making this example self-contained and runnable.
# Minimal Django setup for a self-contained example
# In your project, these settings would be in your project's settings.py
settings.configure(
DEBUG=True,
SECRET_KEY=os.environ.get('DJANGO_SECRET_KEY', 'insecure-secret-key-for-development-only'),
ROOT_URLCONF='__main__',
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ajax_select' # <-- Crucial: Add ajax_select to your INSTALLED_APPS
],
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
],
STATIC_URL='/static/',
STATIC_ROOT=os.path.join(os.getcwd(), 'staticfiles'), # Required for collectstatic
# Optional: Configure AJAX_SELECT_BOOTSTRAP = True for Bootstrap theme if using it
)
django.setup()
# 1. Define simple models (in your app's models.py)
class Author(models.Model):
name = models.CharField(max_length=200)
birth_year = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')
published_year = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.title
# 2. Create a lookup channel (in your app's lookup_channels.py or similar module)
from ajax_select import LookupChannel, register
@register('authors') # <-- 'authors' is the channel name you'll reference
class AuthorLookup(LookupChannel):
model = Author
search_field = 'name' # Field to search in the model
def get_query(self, q, request):
# Customize your query here
return self.model.objects.filter(name__icontains=q).order_by('name')
def format_item_display(self, obj):
# Customize how results are displayed in the dropdown
return f"{obj.name} ({obj.birth_year or 'N/A'})"
# 3. Register your models with the Admin and apply ajax_select (in your app's admin.py)
from ajax_select.admin import AjaxSelectAdmin
from ajax_select import make_ajax_form
@admin.register(Book)
class BookAdmin(AjaxSelectAdmin):
# Use make_ajax_form to integrate the lookup channel with a ForeignKey
form = make_ajax_form(Book, {'author': 'authors'}) # 'author' is the FK field, 'authors' is the lookup channel name
admin.site.register(Author)
# No need to register Book if using the @admin.register decorator
# 4. Define URL patterns (in your project's urls.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('ajax_select/', include('ajax_select.urls')), # <-- Crucial: Include ajax_select URLs
]
# Example of how you would typically run this in a Django project:
# 1. python manage.py makemigrations myapp
# 2. python manage.py migrate
# 3. python manage.py createsuperuser (to access admin)
# 4. python manage.py runserver
# Then navigate to /admin/ and add a Book to see the autocomplete in action.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ajax_select'
The 'ajax_select' application is not included in Django's `INSTALLED_APPS`.
fixAdd `'ajax_select'` to your `INSTALLED_APPS` list in `settings.py`.
django.urls.exceptions.NoReverseMatch: 'ajax_lookup' is not a registered namespace
The URL patterns for `django-ajax-selects` are not included in your project's `urls.py`.
fixAdd `path('ajax_select/', include('ajax_select.urls'))` to your project's `urlpatterns` in `urls.py`. TypeError: Cannot read properties of undefined (reading 'jQuery') or Cannot read properties of undefined (reading 'django')
This often indicates a JavaScript conflict or that jQuery/jQuery UI is not properly loaded, or loaded in a way that conflicts with `django-ajax-selects`. The 'django' object issue was specifically fixed in v3.0.3, but similar problems can arise from other JS conflicts.
fixEnsure `django-ajax-selects` is installed and `INSTALLED_APPS` and `urls.py` are configured correctly. If on an older version, upgrade to 3.0.3+. Check if other apps or your base templates are loading conflicting jQuery/jQuery UI versions. For versions 3.0.0+, `django-ajax-selects` bundles its own JS/CSS; remove redundant includes.
django.core.exceptions.ImproperlyConfigured: 'my_channel_name' is not a valid lookup channel.
The lookup channel name specified (e.g., in `make_ajax_form`, `AutoCompleteSelect`, or `AutoCompleteSelectMultiple`) either does not exist or has not been properly registered with `@register('my_channel_name')` or configured in `settings.AJAX_LOOKUP_CHANNELS`.
fixVerify that your `LookupChannel` class is decorated with `@register('my_channel_name')` and that 'my_channel_name' exactly matches the string used in your form or admin configuration. Upgrade
Version history
3.0.3latest on PyPI · released Sep 14, 2024
Audit
Dependencies
DjangorequiredThis is a Django application and requires a compatible Django version (3.2 to 5.x for v3.x+).