|
|
|
|
@ -1,47 +1,49 @@
|
|
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.utils.html import format_html
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from django.contrib import admin# 导入Django admin相关模块,用于自定义后台管理
|
|
|
|
|
from django.urls import reverse# 导入reverse函数,用于生成URL反向解析
|
|
|
|
|
from django.utils.html import format_html# 导入format_html,用于安全生成HTML内容(防止XSS攻击)
|
|
|
|
|
from django.utils.translation import gettext_lazy as _# 导入gettext_lazy,用于国际化翻译(惰性加载,优化性能)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(is_enable=False)
|
|
|
|
|
def disable_commentstatus(modeladmin, request, queryset):# 自定义批量操作:禁用评论状态
|
|
|
|
|
queryset.update(is_enable=False)# 将选中的评论记录批量更新is_enable字段为False(禁用)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def enable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
queryset.update(is_enable=True)
|
|
|
|
|
def enable_commentstatus(modeladmin, request, queryset):# 自定义批量操作:启用评论状态
|
|
|
|
|
queryset.update(is_enable=True) # 将选中的评论记录批量更新is_enable字段为True(启用)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disable_commentstatus.short_description = _('Disable comments')
|
|
|
|
|
disable_commentstatus.short_description = _('Disable comments')# 为批量操作设置显示名称(支持国际化)
|
|
|
|
|
enable_commentstatus.short_description = _('Enable comments')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 自定义评论模型的Admin管理类,控制后台评论的展示和操作
|
|
|
|
|
class CommentAdmin(admin.ModelAdmin):
|
|
|
|
|
list_per_page = 20
|
|
|
|
|
list_display = (
|
|
|
|
|
list_per_page = 20# 每页显示20条评论记录
|
|
|
|
|
list_display = (# 列表页展示的字段(自定义字段需在类中定义对应方法)
|
|
|
|
|
'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):
|
|
|
|
|
info = (obj.author._meta.app_label, obj.author._meta.model_name)
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.author.id,))
|
|
|
|
|
return format_html(
|
|
|
|
|
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):# 自定义字段:生成关联用户的后台编辑链接
|
|
|
|
|
info = (obj.author._meta.app_label, obj.author._meta.model_name)# 获取用户模型的app标签和模型名称(用于生成admin URL)
|
|
|
|
|
# obj.author表示评论关联的用户对象
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.author.id,))# 反向解析生成用户模型的编辑页URL(格式:admin:app_label_model_name_change)
|
|
|
|
|
return format_html( # 生成HTML链接,显示用户昵称(若无昵称则显示邮箱)
|
|
|
|
|
u'<a href="%s">%s</a>' %
|
|
|
|
|
(link, obj.author.nickname if obj.author.nickname else obj.author.email))
|
|
|
|
|
|
|
|
|
|
def link_to_article(self, obj):
|
|
|
|
|
info = (obj.article._meta.app_label, obj.article._meta.model_name)
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
|
|
|
|
|
return format_html(
|
|
|
|
|
def link_to_article(self, obj):# 自定义字段:生成关联文章的后台编辑链接
|
|
|
|
|
info = (obj.article._meta.app_label, obj.article._meta.model_name)# 获取文章模型的app标签和模型名称
|
|
|
|
|
# obj.article表示评论关联的文章对象
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))# 反向解析生成文章模型的编辑页URL
|
|
|
|
|
return format_html(# 生成HTML链接,显示文章标题
|
|
|
|
|
u'<a href="%s">%s</a>' % (link, obj.article.title))
|
|
|
|
|
|
|
|
|
|
link_to_userinfo.short_description = _('User')
|
|
|
|
|
link_to_article.short_description = _('Article')
|
|
|
|
|
# 自定义字段的显示名称(支持国际化)
|
|
|
|
|
link_to_userinfo.short_description = _('User')# 显示为"用户"
|
|
|
|
|
link_to_article.short_description = _('Article')# 显示为"文章"
|
|
|
|
|
|