django-admin-search-field
A per-field search selector for the Django admin changelist.
By default, the Django admin searches the submitted term across every
field listed in a ModelAdmin.search_fields (joined with OR). On tables
with many fields, several relations, or large row counts, that makes the
search slow — and there is no built-in way to search a single field instead.
This package adds a small combobox next to the search box, letting the user restrict the search to one field — or keep "All fields" for the native behaviour.
Features
- Adds a
<select>next to the changelist search box with one option persearch_fieldsentry, using a friendly label (verbose_name, following relations, e.g.author__name→ "Author › Name"). - Restricts the actual search query to the chosen field via the
sfGET parameter (name configurable). - Works globally, for every registered
ModelAdmin, via a single opt-in call — no need to touch eachadmin.py. - Also available as an explicit mixin (
SearchFieldSelectMixin) if you'd rather opt in perModelAdmin. - Defensive by design: any unexpected failure falls back to Django's native search behaviour instead of breaking the admin page.
- Ships a template override (
admin/search_form.html) and a small, dependency-free CSS file that follows the admin's own light/dark theme variables.
Installation
pip install django-admin-search-field
Add the app to INSTALLED_APPS, before django.contrib.admin (required
so the bundled admin/search_form.html overrides Django's default template
via the app_directories template loader):
INSTALLED_APPS = [
"django_admin_search_field",
"django.contrib.admin",
...
]
Enable the selector once, globally — e.g. from your own app's
AppConfig.ready():
# myapp/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = "myapp"
def ready(self):
from django_admin_search_field import install_search_field_selector
install_search_field_selector()
(Optional) include the bundled CSS in your admin/base_site.html (or
wherever you already extend the admin base template):
{% load static %}
<link rel="stylesheet" href="{% static 'django_admin_search_field/css/search_field.css' %}">
That's it — every ModelAdmin with search_fields configured now shows the
field selector.
Usage without the global monkey-patch
If you'd rather enable this on a single ModelAdmin, skip
install_search_field_selector() and use the mixin instead:
from django.contrib import admin
from django_admin_search_field import SearchFieldSelectMixin
from myapp.models import Book
@admin.register(Book)
class BookAdmin(SearchFieldSelectMixin, admin.ModelAdmin):
search_fields = ["title", "isbn", "author__name"]
In this mode you're responsible for including the CSS/template yourself,
and you don't need to add django_admin_search_field to INSTALLED_APPS —
only the Python mixin is used.
Configuration
| Setting | Default | Description |
|---|---|---|
ADMIN_SEARCH_FIELD_VAR |
"sf" |
Name of the GET parameter used to carry the chosen field. Override it if sf collides with something else in your project. |
How it works
resolve_search_fields()restrictssearch_fieldsto the field selected via thesfGET parameter — or returns the original list unchanged whensfis empty ("All fields") or points to a field that isn't configured.sfis registered in the changelist'sIGNORED_PARAMS, so it's never misread as a list filter (which would otherwise raiseIncorrectLookupParameters).- The template only receives the
cl(ChangeList) object, so the combobox data is attached directly to it:cl.search_field_choices,cl.search_field_selected,cl.search_field_var.
Development
pip install -e ".[dev]"
pytest
Compatibility
- Python 3.10+
- Django 4.2+
License
MIT — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_admin_search_field-0.1.1.tar.gz.
File metadata
- Download URL: django_admin_search_field-0.1.1.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b05d8df70b0622cb865718c451a4287bda0548ac0725258acedd28f7bb5b8f7a
|
|
| MD5 |
03ae3b83491374c1148e7463887586e0
|
|
| BLAKE2b-256 |
6f4aa338d7a364d68dc17178d9145d47a128989ae8e405fe91204bb679dc4824
|
File details
Details for the file django_admin_search_field-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_admin_search_field-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d8bde859f6cc913fbab305f207ad670079c817851ea154979241a1779456ae7
|
|
| MD5 |
b497c76905b721664c0896f67c0cb702
|
|
| BLAKE2b-256 |
15d764e18271fd04e9f040b10b9933126a6e73752590ffdd2d1fd20c97cc88d7
|