You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DjangoBlog/comments/admin.py

30 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#zy:comments/admin.py
#zy: 评论(Comment)模型的管理后台配置文件
#zy: 该文件用于配置Django管理后台中评论模型的显示和行为
#zy: 导入Django管理后台模块用于注册模型和管理配置
from django.contrib import admin
#zy: 从当前包(models模块)导入Comment模型
from .models import Comment
#zy: 使用装饰器注册Comment模型到管理后台并指定使用CommentAdmin作为管理类
@admin.register(Comment)
#zy: 定义评论模型的管理配置类继承自admin.ModelAdmin
class CommentAdmin(admin.ModelAdmin):
#zy: 设置在管理后台列表页面显示的字段列
# 将显示:作者(author)、关联文章(post)、创建时间(created_time)
list_display = ['author', 'post', 'created_time']
# zy:设置右侧过滤侧边栏的过滤条件
#zy: 允许用户按照创建时间(created_time)对评论进行筛选
list_filter = ['created_time']
# zy:设置搜索字段,在列表页顶部显示搜索框
# zy:支持按作者用户名(author__username)和评论内容(content)进行搜索
# author__username表示通过外键关系搜索作者的用户名字段
search_fields = ['author__username', 'content']
# zy:设置只读字段,在编辑页面中这些字段将显示为不可编辑状态
# zy:创建时间(created_time)通常应该设为只读,防止用户修改
readonly_fields = ['created_time']