django admin site search
1.1.0
Django 管理員的全域/網站搜尋模式。
model_char_fields
(預設):所有CharField
(和子類別)值,帶有__icontains
。admin_search_fields
:呼叫每個 ModelAdmin 的 get_search_results(...) 方法。pip install django-admin-site-search
。admin_site_search
加入您的INSTALLED_APPS
設定中。AdminSiteSearchView
新增至您的 AdminSite: from django . contrib import admin
from admin_site_search . views import AdminSiteSearchView
class MyAdminSite ( AdminSiteSearchView , admin . AdminSite ):
...
templates/
目錄中建立admin/base_site.html
。templates/
目錄位於應用程式內部,則該應用程式必須出現在INSTALLED_APPS
中,位於您的自訂管理應用程式之前。admin_site_search
模板: {% 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 %}
admin_site_search/head.html
也載入 Alpine JS。/static/
中,以避免外部依賴。modal.html
和button.html
的放置並不嚴格,儘管前者最好位於頂級位置。{% block header %}
- 這比footer
更好。 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"
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"""
將TextField
位結果新增至搜尋。
from django . contrib import admin
from django . db . models import Q , Field , TextField
from admin_site_search . views import AdminSiteSearchView
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 )
請注意,出於效能原因,預設不會這樣做:大量文字條目上的__icontains
不是最佳選擇。