💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] >[success] # 展示页面的字段配置 ![](https://box.kancloud.cn/a769d7085eeb08b61636685bc7869716_1424x482.png) ~~~ @admin.register(Post, site=custom_site) class PostAdmin(admin.ModelAdmin): list_display = ['title', 'category', 'status', 'owner', 'created_time'] search_fields = ['title', 'owner__username'] list_filter = ['title','category'] show_full_result_count = False list_display_links = ['status'] actions_on_top = False actions_on_bottom = True date_hierarchy = 'created_time' list_editable = ('category',) ~~~ >[danger] ##### list_display 显示展示字段 ~~~ 1.不使用的时候,默认返回的是对应的model.py的类对象,一般用__str__,返回想 展示的名字,默认list_display = ('__str__',) 2.使用时候展示指定字段 ~~~ * 不使用 ![](https://box.kancloud.cn/da6f519503de881932b449a4791e6052_1375x253.png) * 使用 ![](https://box.kancloud.cn/cde59485cae29f0c2358f3439fe65edf_1418x271.png) >[danger] ##### search_fields 搜索字段 ~~~ 1.使用后可以指定 能被搜索的字段,多表时候,跨表要用对应例如: owner__username 2.不使用则不展示 功能搜索 ~~~ * 不使用 ![](https://box.kancloud.cn/da6f519503de881932b449a4791e6052_1375x253.png) * 使用 ![](https://box.kancloud.cn/89eac8150d24f1ebbb6970c2d114fad9_1343x286.png) >[danger] ##### list_filter 对指定字段进行分类过滤 ~~~ 1.想对指定字段的,分类内容过滤使用 ~~~ * 使用后效果 ![](https://box.kancloud.cn/9088caef78977bdfb121b40039e78766_1432x374.png) >[danger] ##### show_full_result_count 查询后展示过滤条数信息 ~~~ 1.默认为True 开启的状态 show_full_result_count = True 2.建议设置成flase,减少不必要的请求 ~~~ * 默认时候 ![](https://box.kancloud.cn/e6f3e9b54e2b51c215acb399f99a6089_1428x433.png) * 设置成false ![](https://box.kancloud.cn/51f8676a9ee4e9556c3b2f80e59b6b3f_1018x260.png) >[danger] ##### list_display_links 指定科跳转的字段 ~~~ 1.指定点击字段跳转到编辑页时 ~~~ ![](https://box.kancloud.cn/96743580990bed7f057bb01921432182_903x277.png) >[danger] ##### actions_on_top/bottom ~~~ 1.设置操作,栏的展示 默认actions_on_top= True 2.想底部展示 actions_on_tbottom = True ~~~ * top ![](https://box.kancloud.cn/0c1af6e61c9c9c50a54730264118f39c_1398x326.png) * bottom ![](https://box.kancloud.cn/0f6cd9f9b230320eec504eb0fb504ed5_1115x226.png) >[danger] ##### date_hierarchy 显示创建时间点 ~~~ 1.显示创建的时间跳转 ~~~ ![](https://box.kancloud.cn/e94d14c15325b711311664f9d1232224_1130x292.png) >[danger] ##### list_editable 直接可编辑 ![](https://box.kancloud.cn/bfb93c13851495f5c48231d00a70091c_1134x308.png) >[danger] ##### 自定义字段显示 ~~~ 1.在model 层定义,通过构造一个方法,生成标题 status = models.PositiveIntegerField(default=1, choices=STATUS_ITEMS, verbose_name="状态") def status_show(self): return "当前状态%s"%self.status status_show.short_description = "展示状态" ~~~ ![](https://box.kancloud.cn/201cb7fc820c25a8f6e22f2c5d874a11_1135x359.png) >[danger] #### 自定义 编辑按钮 `from django.utils.html import format_html` ~~~ 1.通过 在admin 中定义方法 operator 来自定义按钮,注意要把operator加入 list_display 2.reverse('cus_admin:blong_post_change', args=(obj.id,)) 第一个cus_admin 是我们生成的自定制的custom_site 定义的名字,后面blong 是app post 是请求方式,change 是对编辑做请求 3.写法 def operator(self, obj): return format_html( '<a href="{}">编辑</a>', reverse('cus_admin:blong_post_change', args=(obj.id,)) ) # operator.allow_tags = True # 用format_html替代 operator.short_description = '操作' ~~~ ![](https://box.kancloud.cn/052d61543d460f9aae862b3cc0a83fd5_1367x260.png)