后台管理有时候会有针对于自定义字段的搜索,这时候Django自带的则无法满足,所以会有重写的额需求
- 继承ModelAdmin
- 重写get_search_results
class ImageTableAdmin(AjaxAdmin,ImportExportModelAdmin):
actions = ['btn_async_resource'] # 'btn_import',
list_display = ('id','image_name_show','thumb_image_show','origin_image_show', 'image_type')
# 添加搜索字段
search_fields = ['image_name', 'image_type__image_type_name','image_type__image_type_chinese_name']
resource_class = ImageTableResource
def get_search_results(self, request, queryset, search_term):
# 增加自己的重写
queryset, use_distinct = super(ImageTableAdmin, self).get_search_results(request, queryset, search_term)
return queryset, use_distinct