diff --git a/software-engineering-methodology/src/DjangoBlog-master(1)/DjangoBlog-master/comments/admin.py b/software-engineering-methodology/src/DjangoBlog-master(1)/DjangoBlog-master/comments/admin.py
index a814f3f..fc9d376 100644
--- a/software-engineering-methodology/src/DjangoBlog-master(1)/DjangoBlog-master/comments/admin.py
+++ b/software-engineering-methodology/src/DjangoBlog-master(1)/DjangoBlog-master/comments/admin.py
@@ -1,47 +1,95 @@
+# 导入Django admin相关模块,用于自定义后台管理界面
from django.contrib import admin
+# 导入reverse函数,用于生成URL反向解析
from django.urls import reverse
+# 导入format_html,用于在admin中生成HTML代码
from django.utils.html import format_html
+# 导入gettext_lazy,用于国际化翻译
from django.utils.translation import gettext_lazy as _
def disable_commentstatus(modeladmin, request, queryset):
+ """
+ 自定义admin批量操作:禁用选中的评论
+ modeladmin: 当前模型的admin实例
+ request: 当前请求对象
+ queryset: 选中的记录集合
+ """
queryset.update(is_enable=False)
def enable_commentstatus(modeladmin, request, queryset):
+ """
+ 自定义admin批量操作:启用选中的评论
+ modeladmin: 当前模型的admin实例
+ request: 当前请求对象
+ queryset: 选中的记录集合
+ """
queryset.update(is_enable=True)
+# 为批量操作设置显示名称(支持国际化)
disable_commentstatus.short_description = _('Disable comments')
enable_commentstatus.short_description = _('Enable comments')
class CommentAdmin(admin.ModelAdmin):
+ """
+ 评论模型(Comment)在Django Admin中的管理配置类
+ 用于自定义评论模型在后台的展示和操作方式
+ """
+ # 每页显示20条记录
list_per_page = 20
+
+ # 列表页显示的字段
list_display = (
- 'id',
- 'body',
- 'link_to_userinfo',
- 'link_to_article',
- 'is_enable',
- 'creation_time')
+ 'id', # 评论ID
+ 'body', # 评论内容
+ 'link_to_userinfo', # 关联的用户(自定义字段,显示链接)
+ 'link_to_article', # 关联的文章(自定义字段,显示链接)
+ 'is_enable', # 是否启用
+ 'creation_time' # 创建时间
+ )
+
+ # 列表页中可点击跳转详情页的字段
list_display_links = ('id', 'body', 'is_enable')
+
+ # 右侧过滤条件字段
list_filter = ('is_enable',)
+
+ # 编辑页中排除的字段(不允许编辑,通常是自动生成的字段)
exclude = ('creation_time', 'last_modify_time')
+
+ # 注册自定义的批量操作
actions = [disable_commentstatus, enable_commentstatus]
def link_to_userinfo(self, obj):
+ """
+ 自定义列表字段:显示评论作者的链接(跳转到用户详情页)
+ obj: 当前评论对象实例
+ """
+ # 获取用户模型的app标签和模型名称,用于生成URL
info = (obj.author._meta.app_label, obj.author._meta.model_name)
+ # 生成用户详情页的admin URL
link = reverse('admin:%s_%s_change' % info, args=(obj.author.id,))
+ # 返回HTML链接,显示用户昵称(如果没有则显示邮箱)
return format_html(
u'%s' %
(link, obj.author.nickname if obj.author.nickname else obj.author.email))
def link_to_article(self, obj):
+ """
+ 自定义列表字段:显示评论所属文章的链接(跳转到文章详情页)
+ obj: 当前评论对象实例
+ """
+ # 获取文章模型的app标签和模型名称,用于生成URL
info = (obj.article._meta.app_label, obj.article._meta.model_name)
+ # 生成文章详情页的admin URL
link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
+ # 返回HTML链接,显示文章标题
return format_html(
u'%s' % (link, obj.article.title))
+ # 自定义字段在列表页的显示名称(支持国际化)
link_to_userinfo.short_description = _('User')
- link_to_article.short_description = _('Article')
+ link_to_article.short_description = _('Article')
\ No newline at end of file