Along with styles, admin_site_search/head.html loads Alpine JS.
This is bundled into /static/, to avoid external dependencies.
The placement of modal.html and button.html are not strict, though the former would ideally be in a top-level
position.
Django 4.x exposes {% block header %} - this is preferable to footer.
Customisation
Class attributes
class MyAdminSite(AdminSiteSearchView, admin.AdminSite):
# Sets the last part of the search route (`<admin_path>/search/`).
site_search_path: str = "search/"
# Set the search method/behaviour.
site_search_method: Literal["model_char_fields", "admin_search_fields"] = "model_char_fields"
Methods
def match_app(
self, request, query: str, name: str
) -> bool:
"""DEFAULT: case-insensitive match the app name"""
def match_model(
self, request, query: str, name: str, object_name: str, fields: List[Field]
) -> bool:
"""DEFAULT: case-insensitive match the model and field attributes"""
def match_objects(
self, request, 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, request, query: str, field: Field
) -> Optional[Q]:
"""DEFAULT: Returns a Q 'icontains' filter for Char fields, otherwise None
Note: this method is only invoked if model_char_fields is the site_search_method."""
def get_model_queryset(
self, request, model_class: Model, model_admin: Optional[ModelAdmin]
) -> QuerySet:
"""DEFAULT: Returns the model class' .objects.all() queryset."""
def get_model_class(
self, request, app_label: str, model_dict: dict
) -> Optional[Model]:
"""DEFAULT: Retrieve the model class from the dict created by admin.AdminSite"""
Examples
1. Skip models from search.
class CustomAdminSite(AdminSiteSearchView, admin.AdminSite):
def get_model_class(self, *args, **kwargs) -> Optional[Model]:
"""Extends super() to skip the auth.User model"""
model_class = super().get_model_class(*args, **kwargs)
model_name = f"{model_class._meta.app_label}.{model_class._meta.object_name}"
if model_name == "auth.User":
return None
return model_class
This can be adapted to skip multiple models, or applications.
2. Add TextField results to search.
class MyAdminSite(AdminSiteSearchView, admin.AdminSite):
site_search_method: "model_char_fields"
def filter_field(self, request, 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 suboptimal.