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.
 

112 lines
3.2 KiB

from django.contrib import admin
from post.models import Topic, Comment
# Register your models here.
# method 1
# admin.site.register([Topic, Comment])
# method 2
# class TopicAdmin(admin.ModelAdmin):
# pass
#
#
# class CommentAdmin(admin.ModelAdmin):
# pass
#
#
# admin.site.register(Topic, TopicAdmin)
# admin.site.register(Comment, CommentAdmin)
# method 3
@admin.register(Topic)
class TopicAdmin(admin.ModelAdmin):
# 列表页面定制
list_display = ('title', 'topic_content', 'topic_is_online', 'user')
actions = ['topic_online', 'topic_offline']
search_fields = ['title', 'user__username']
ordering = ['title']
list_per_page = 1
list_max_show_all = 5
# changeForm页面定制
# fields = ['user', 'title', 'is_online'] # 设置编辑新增页面的元素信息
# fields = [('user', 'title'), 'content', 'is_online'] # 设置编辑新增页面的元素信息
# exclude = ['user'] # 设置编辑新增页面不显示的元素
def topic_online(self, request, queryset):
rows_updated = queryset.update(is_online=True)
self.message_user(request, '%s topic online' % rows_updated)
topic_online.short_description = u'上线所选的 %s' % Topic._meta.verbose_name
def topic_offline(self, request, queryset):
row_updated = queryset.update(is_online=False)
self.message_user(request, '%s topics offline' % row_updated)
topic_offline.short_description = u'下线所选的 %s' % Topic._meta.verbose_name
def topic_is_online(self, obj):
return u'' if obj.is_online else u''
topic_is_online.short_description = u'话题是否在线'
def topic_content(self, obj):
return obj.content[:30]
topic_content.short_description = u'话题内容'
class TitleFilter(admin.SimpleListFilter):
title = ('标题过滤')
parameter_name = 'tf'
def lookups(self, request, model_admin):
return (
('001', ('包含001')),
('!001', ('不包含001')),
)
def queryset(self, request, queryset):
if self.value() == '001':
return queryset.filter(title__contains=self.value())
elif self.value() == '!001':
return queryset.exclude(title__contains=self.value()[1:])
else:
return queryset
list_filter = [TitleFilter, 'user__username']
def get_ordering(self, request):
if request.user.is_superuser:
return ['id']
else:
return self.ordering
# def get_queryset(self, request):
# return self.model._default_manager.filter(title__contains='1')
# changeForm页面定制 深度定制分组
fieldsets = (
(
'Topic Part A',
{
'fields': ('title', 'user'),
'description': 'Topic的Title和User'
}
),
(
'Topic Part B',
{
'fields': ('content', 'is_online'),
'classed': ['collapse', 'wide'],
'description': 'Topic的Content和is_online'
}
)
)
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
pass