8、Django的后台边栏筛选工具简单定制list_filter
list_filter 边框筛选工具,实现按title、author、content过滤。
代码:
from django.contrib import admin from blog.models import Blog # Register your models here. @admin.register(Blog) class BlogAdmin(admin.ModelAdmin): # list_display用于设置列表页面要显示的不同字段 list_display = ['title','author','author_name','author_last_name','author_email'] # search_fields用于设置搜索栏中要搜索的不同字段 search_fields = ['title','author__username','content'] # 默认作者,不能随便修改作者,readonly_fields表示只能读取当今设置的字段 readonly_fields = ['author'] def save_model(self, request, obj, form, change): # 如果不是修改,那就是"新建"的时候 # change=False时代表这个记录是新建的,为True表示这个记录执行修改 if not change: obj.author = request.user super(BlogAdmin,self).save_model(request,obj,form,change) # list_filter 边框筛选工具,比如可以设置为按作者进行筛选,常用的有选择的字段筛选、时间的过滤 list_filter = ['title','author','content']