{"id":5902,"library":"django-admin-autocomplete-filter","title":"Django Admin Autocomplete Filter","description":"A simple Django app to render list filters in django admin using an autocomplete widget. It leverages Django's built-in `autocomplete_fields` functionality for foreign key and many-to-many relationships. The library is actively maintained, with minor releases for bug fixes and major releases for new features and improvements. Current version is 0.7.1.","status":"active","version":"0.7.1","language":"en","source_language":"en","source_url":"https://github.com/farhan0581/django-admin-autocomplete-filter","tags":["django","admin","autocomplete","filter","foreignkey","manytomany"],"install":[{"cmd":"pip install django-admin-autocomplete-filter","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This is a Django admin application and requires Django version >= 2.0.","package":"Django","optional":false}],"imports":[{"symbol":"AutocompleteFilter","correct":"from admin_auto_filters.filters import AutocompleteFilter"},{"symbol":"AutocompleteFilterFactory","correct":"from admin_auto_filters.filters import AutocompleteFilterFactory"}],"quickstart":{"code":"import os\nimport django\nfrom django.conf import settings\nfrom django.db import models\nfrom django.contrib import admin\n\nsettings.configure(\n    INSTALLED_APPS=[\n        'django.contrib.admin',\n        'django.contrib.auth',\n        'django.contrib.contenttypes',\n        'django.contrib.sessions',\n        'django.contrib.messages',\n        'django.contrib.staticfiles',\n        'admin_auto_filters', # Add this app\n        'my_app', # Your app name\n    ],\n    SECRET_KEY=os.environ.get('DJANGO_SECRET_KEY', 'a-very-secret-key-for-dev'),\n    TEMPLATES=[\n        {\n            'BACKEND': 'django.template.backends.django.DjangoTemplates',\n            'APP_DIRS': True,\n            'OPTIONS': {\n                'context_processors': [\n                    'django.template.context_processors.debug',\n                    'django.template.context_processors.request',\n                    'django.contrib.auth.context_processors.auth',\n                    'django.contrib.messages.context_processors.messages',\n                ],\n            },\n        },\n    ],\n    DATABASES={\n        'default': {\n            'ENGINE': 'django.db.backends.sqlite3',\n            'NAME': ':memory:',\n        }\n    },\n    STATIC_URL='/static/',\n    ROOT_URLCONF=__name__,\n    DEBUG=True\n)\ndjango.setup()\n\n# models.py example\nclass Artist(models.Model):\n    name = models.CharField(max_length=128)\n\n    def __str__(self):\n        return self.name\n\nclass Album(models.Model):\n    name = models.CharField(max_length=64)\n    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)\n\n    def __str__(self):\n        return self.name\n\n# admin.py example\nfrom admin_auto_filters.filters import AutocompleteFilter\n\nclass ArtistFilter(AutocompleteFilter):\n    title = 'Artist' # display title\n    field_name = 'artist' # name of the foreign key field\n\n@admin.register(Artist)\nclass ArtistAdmin(admin.ModelAdmin):\n    search_fields = ['name'] # REQUIRED for Django's autocomplete functionality\n\n@admin.register(Album)\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [ArtistFilter]\n\n# Minimal URLConf for admin\nfrom django.urls import path\nfrom django.contrib import admin\n\nurlpatterns = [\n    path('admin/', admin.site.urls),\n]\n\n# To make it runnable for demonstration (normally run via manage.py runserver)\nif __name__ == '__main__':\n    print(\"Django Admin Autocomplete Filter setup example.\")\n    print(\"To see it in action, you'd typically run 'python manage.py runserver'\")\n    print(\"and navigate to the Django admin interface (e.g., /admin/album/)\")\n    print(\"You'll need to create a superuser and some Artist/Album objects.\")\n\n    # Example of how you would apply migrations and create a superuser\n    # from django.core.management import call_command\n    # call_command('makemigrations', 'my_app')\n    # call_command('migrate')\n    # call_command('createsuperuser') # follow prompts\n\n","lang":"python","description":"To use `django-admin-autocomplete-filter`, first add `admin_auto_filters` to your `INSTALLED_APPS`. Then, define `search_fields` on the `ModelAdmin` for the related model you wish to filter by. Finally, use `AutocompleteFilter` or `AutocompleteFilterFactory` in the `list_filter` of the `ModelAdmin` where you want the autocomplete filter to appear."},"warnings":[{"fix":"Upgrade to version 0.6.1 or higher (e.g., `pip install --upgrade django-admin-autocomplete-filter`).","message":"Version 0.6 introduced a bug in its JavaScript files, requiring an immediate patch in version 0.6.1. Users upgrading to 0.6 should ensure they update to 0.6.1 or later to avoid front-end issues.","severity":"breaking","affected_versions":"0.6"},{"fix":"Ensure `search_fields` is properly defined in the `ModelAdmin` class of the related model (e.g., `search_fields = ['name']` in `ArtistAdmin`).","message":"For the autocomplete filter to function correctly, the `ModelAdmin` of the *related model* (the one being filtered by, e.g., `ArtistAdmin` when filtering `Album` by `Artist`) MUST have `search_fields` defined. Without this, you will encounter 'Reverse for '<app_name>_<model_name>_autocomplete' not found' errors or autocomplete results will fail to load.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If custom filtering is required, implement it within the related `ModelAdmin`'s `get_queryset` method, potentially with conditional logic to distinguish between standard requests and autocomplete requests.","message":"When a field is configured as an autocomplete field in Django Admin, the `get_queryset` method of the related model's `ModelAdmin` is directly called to fetch results. This can bypass and effectively override `ModelForm` filtering logic defined in `__init__` or `clean` methods, leading to unexpected filter behavior or invalid choices appearing in the autocomplete dropdown.","severity":"gotcha","affected_versions":"All versions (inherent to Django's autocomplete_fields interaction)"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}