A search (cmd+k) modal, for the Django admin UI, that searches your entire site.
Project description
django-admin-site-search
A global/site search modal for the Django admin.
Features
- 🎩 Works out-of-the-box, with minimal config.
- 🔎 Search performed on:
- App labels.
- Model labels and field attributes.
- CharField values (with
__icontains
).- Subclasses also included:
SlugField
,URLField
, etc.
- Subclasses also included:
- 🔒 Built-in auth: users can only search apps and models that they have permission to view.
- ⚡ Results appear on-type, with throttling/debouncing to avoid excessive requests.
- 🎹 Keyboard navigation (cmd+k, up/down, enter).
- ✨ Responsive, and supports dark/light mode.
- Django's built-in CSS vars are used to match your admin theme.
Requirements
- Python 3.8 - 3.11.
- Django 3.2 - 4.1.
Setup
1. Install
- Install with your package manager, e.g.
pip install django-admin-site-search
. - Add
admin_site_search
to yourINSTALLED_APPS
setting.
2. Add View
- If you haven't already, override/extend the default AdminSite.
- Add the
AdminSiteSearchView
to your AdminSite:
from django.contrib import admin
from admin_site_search.views import AdminSiteSearchView
class MyAdminSite(AdminSiteSearchView, admin.AdminSite):
...
3. Add Templates
- If you haven't already, create
admin/base_site.html
in yourtemplates/
directory. - Include the
admin_site_search
templates:
{% extends "admin/base_site.html" %}
{% block extrahead %}
{% include 'admin_site_search/head.html' %}
{{ block.super }}
{% endblock %}
{% block footer %}
{{ block.super }}
{% include 'admin_site_search/modal.html' %}
{% endblock %}
{% block usertools %}
{% include 'admin_site_search/button.html' %}
{{ block.super }}
{% endblock %}
Notes
- Along with styles,
admin_site_search/head.html
loads Alpine JS.- This is bundled into
/static/
, to avoid external dependencies.
- This is bundled into
- The placement of
modal.html
andbutton.html
are not strict, though the former would ideally be in a top-level position.- Django 4.x exposes
{% block header %}
- this is preferable tofooter
.
- Django 4.x exposes
Customisation
Methods in AdminSiteSearchView
can be extended to add custom logic.
def match_app(self, query: str, name: str) -> bool:
"""DEFAULT: case-insensitive match the app name"""
...
def match_model(
self, query: str, name: str, object_name: str, fields: List[Field]
) -> bool:
"""DEFAULT: case-insensitive match the model and field attributes"""
...
def match_objects(
self, query: str, model_class: Model, model_fields: List[Field]
) -> QuerySet:
"""DEFAULT: Returns the QuerySet after performing an OR filter across all Char fields in the model."""
...
def filter_field(self, query: str, field: Field) -> Optional[Q]:
"""DEFAULT: Returns a Q 'icontains' filter for Char fields, otherwise None"""
...
Example
Add TextField
results to search.
class MyAdminSite(AdminSiteSearchView, admin.AdminSite):
...
def filter_field(self, query: str, field: Field) -> Optional[Q]:
"""Extends super() to add TextField support to site search"""
if isinstance(field, TextField):
return Q(**{f"{field.name}__icontains": query})
return super().filter_field(query, field)
Note that this isn't done by default for performance reasons: __icontains
on a
large number of text entries is inefficient.
Screenshots
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for django-admin-site-search-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a95550f53442e9fe1c2445fdd7dbdb908322a91ad18e8279a1d1f867c2af607 |
|
MD5 | 28b3924dfb2649e0b69e282d091a5edf |
|
BLAKE2b-256 | cb5c5e58821b87151da2e2989ecd32d9e09063620a2f9a0d00367511f9f5a755 |
Close
Hashes for django_admin_site_search-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e644eac5d89a90f1a75fd3e5d22a45d046e3f323b61b53d529d55abea12ad5be |
|
MD5 | b3d53d20c8c1fa334640f7303c136ce1 |
|
BLAKE2b-256 | c74fa2d4682c6a4af33c71894545e1a757f898b40d8cd8648c5ca2704c36e52d |