Skip to main content

django-admin-search-field

Latest on Django Packages

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 per search_fields entry, 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 sf GET parameter (name configurable).
  • Works globally, for every registered ModelAdmin, via a single opt-in call — no need to touch each admin.py.
  • Also available as an explicit mixin (SearchFieldSelectMixin) if you'd rather opt in per ModelAdmin.
  • 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() restricts search_fields to the field selected via the sf GET parameter — or returns the original list unchanged when sf is empty ("All fields") or points to a field that isn't configured.
  • sf is registered in the changelist's IGNORED_PARAMS, so it's never misread as a list filter (which would otherwise raise IncorrectLookupParameters).
  • 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

django_admin_search_field-0.1.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_search_field-0.1.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_search_field-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_admin_search_field-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b05d8df70b0622cb865718c451a4287bda0548ac0725258acedd28f7bb5b8f7a
MD5 03ae3b83491374c1148e7463887586e0
BLAKE2b-256 6f4aa338d7a364d68dc17178d9145d47a128989ae8e405fe91204bb679dc4824

See more details on using hashes here.

File details

Details for the file django_admin_search_field-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_search_field-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9d8bde859f6cc913fbab305f207ad670079c817851ea154979241a1779456ae7
MD5 b497c76905b721664c0896f67c0cb702
BLAKE2b-256 15d764e18271fd04e9f040b10b9933126a6e73752590ffdd2d1fd20c97cc88d7

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page