update. 完成分类显示与详情页显示效果

This commit is contained in:
2026-04-01 18:54:42 +08:00
parent 777ca50328
commit 414dc0ee95
23 changed files with 359 additions and 707 deletions
+14 -3
View File
@@ -83,18 +83,18 @@ class ArticleForm(forms.ModelForm):
super().__init__(*args, **kwargs)
# 核心代码:过滤 section 字段,只保留有父级的分类(即二级分类)
# 这里的 queryset 会直接作用于 Admin 的下拉选择框
self.fields['section'].queryset = MainMenu.objects.filter(parent__isnull=False, enabled=True)
self.fields['section'].queryset = MainMenu.objects.filter(parent__isnull=False, visible=True)
# 可选:优化显示格式,让用户知道是哪个主板块下的子板块
# 例如显示:"[新闻中心] 国内新闻"
self.fields['section'].label_from_instance = lambda obj: f"[{obj.parent.name}] {obj.name}"
self.fields['section'].label_from_instance = lambda obj: f"[{obj.parent.title}] {obj.title}"
# —————— 文章 ——————
@admin.register(Article)
class ArticleAdmin(ImportExportModelAdmin):
form = ArticleForm # <--- 关联自定义表单
list_display = ('title', 'section', 'author', 'is_published', 'created_at')
list_display = ('title', 'section', 'author', 'is_published', 'created_at', 'attachment_count')
list_filter = ('section', 'is_published', 'created_at')
search_fields = ('title', 'content')
date_hierarchy = 'created_at'
@@ -108,6 +108,17 @@ class ArticleAdmin(ImportExportModelAdmin):
('发布设置', {'fields': ('is_published',)}),
)
# 2. 定义显示附件数量的方法
def attachment_count(self, obj):
# 使用 count() 方法统计数量
count = obj.attachments.count()
if count == 0:
return "-" # 没有附件显示横杠
return f"📎 {count}" # 有附件显示图标和数量
# 3. 设置该列在后台的标题
attachment_count.short_description = "附件"
# —————— 通知(带签收) ——————
@admin.register(Notice)