django admin site search
1.1.0
Django 관리자를 위한 전역/사이트 검색 모달입니다.
model_char_fields
( 기본값 ): __icontains
포함된 모든 CharField
(및 하위 클래스) 값.admin_search_fields
: 각 ModelAdmin의 get_search_results(...) 메서드를 호출합니다.pip install django-admin-site-search
.INSTALLED_APPS
설정에 admin_site_search
추가하세요.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
차선책입니다.