53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
from import_export.admin import ImportExportModelAdmin
|
|
|
|
from .models import NewsSection, News
|
|
|
|
|
|
# Register your models here.
|
|
|
|
|
|
# --- 1. 板块管理配置 ---
|
|
@admin.register(NewsSection)
|
|
class NewsSectionAdmin(ImportExportModelAdmin):
|
|
list_display = ('title', 'code', 'order', 'visible') # 列表显示的字段
|
|
list_editable = ('order', 'visible') # 允许直接在列表页修改排序和状态
|
|
search_fields = ('title', 'code') # 搜索框
|
|
prepopulated_fields = {'code': ('title',)} # 输入名称时自动生成 Slug
|
|
ordering = ('order', 'title') # 默认排序
|
|
|
|
|
|
# --- 2. 文章管理配置 ---
|
|
@admin.register(News)
|
|
class NewsAdmin(ImportExportModelAdmin):
|
|
# 列表页显示
|
|
list_display = ('title', 'section', 'author', 'is_published', 'created_at', 'updated_at')
|
|
list_filter = ('section', 'is_published', 'created_at', 'author') # 右侧过滤器
|
|
search_fields = ('title', 'content') # 搜索框(支持搜标题和内容)
|
|
date_hierarchy = 'created_at' # 顶部日期层级导航
|
|
ordering = ('-created_at',) # 按创建时间倒序
|
|
|
|
# 编辑页布局
|
|
fieldsets = (
|
|
('基本信息', {
|
|
'fields': ('title', 'section', 'author', 'is_published')
|
|
}),
|
|
('内容', {
|
|
# CKEditor5Field 会自动渲染为富文本编辑器,无需额外配置 widget
|
|
'fields': ('content', 'cover'),
|
|
'classes': ('wide',) # wide 类可以让编辑器占据更多宽度
|
|
}),
|
|
('系统信息', {
|
|
'fields': ('created_at', 'updated_at'),
|
|
'classes': ('collapse', 'extrapretty'), # collapse 默认折叠,extrapretty 美化
|
|
'description': '只读信息,自动生成'
|
|
}),
|
|
)
|
|
|
|
# 只读字段(系统自动生成的字段)
|
|
readonly_fields = ('created_at', 'updated_at')
|
|
|
|
# 自动填充 Slug(如果以后给 News 加 slug 字段可以用)
|
|
# prepopulated_fields = {'slug': ('title',)}
|