text: from __future__ import unicode_literals
  
  from django.contrib import admin
  from django.urls import reverse
  from django.utils.html import format_html
  from django.contrib.admin.models import LogEntry
  
  from .models import Post, Category, Tag
  from .adminforms import PostAdminForm
  from typeidea.custom_site import custom_site
  from typeidea.base_admin import BaseOwnerAdmin
  
  
  class PostInline(admin.TabularInline):  # StackedInline  样式不同
      fields = ('title', 'desc')
      extra = 1  # 控制额外多几个
      model = Post
  
  
  @admin.register(Category, site=custom_site)
  class CategoryAdmin(BaseOwnerAdmin):
      inlines = [PostInline, ]
      list_display = ('name', 'status', 'is_nav', 'owner',
                      'post_count', 'created_time')
      fields = ('name', 'status', 'is_nav')
  
      def post_count(self, obj):
          return obj.post_set.count()
  
      post_count.short_description = '文章数量'
  
  
  @admin.register(Tag, site=custom_site)
  class TagAdmin(BaseOwnerAdmin):
      list_display = ('name', 'status', 'owner', 'created_time')
      fields = ('name', 'status')
  
  
  # 这个案例非常重要，到后面的学习中需要着重学习
  # 过滤列表结果，管理界面中的右侧过滤器
  class CategoryOwnerFilter(admin.SimpleListFilter):
      """ 自定义过滤器只展示当前用户分类 """
  
      title = '分类过滤器'
      parameter_name = 'owner_category'
  
      def lookups(self, request, model_admin):
          return Category.objects.filter(owner=request.user).values_list('id', 'name')
  
      def queryset(self, request, queryset):
          category_id = self.value()
          if category_id:
              return queryset.filter(category_id=self.value())
          return queryset
  
  
  @admin.register(Post, site=custom_site)
  class PostAdmin(BaseOwnerAdmin):
      form = PostAdminForm
      list_display = ['title', 'show_category_name', 'status',
                      'owner', 'created_time', 'operator'
                      ]
      list_display_links = []
  
      #list_filter = ['category']
      list_filter = [CategoryOwnerFilter, ]
      search_fields = ['title', 'category_name']
  
      actions_on_top = True
      actions_on_bottom = True
  
      # 编辑页面
      save_on_top = True
      exclude = ['owner', ]
  
      # 分段显示方式，更为清晰和方便
      fieldsets = (
          ('基础配置', {
              'description': '基础配置描述',
              'fields': (
                  ('title', 'category'),
                  'status',
              ),
          }),
          ('内容', {
              'fields': (
                  'desc',
                  'content',
              ),
          }),
          ('额外信息', {
              'classes': ('wide',),
              'fields': ('tag', ),
          })
      )
      filter_horizontal = ('tag', )  # 用于控制多对多字段选择的效果
  
      def operator(self, obj):
          return format_html(
              '<a href="{}">编辑</a>',
              reverse('cus_admin:blog_post_change', args=(obj.id,))
          )
      operator.short_description = '操作'
  
      # 在列表中显示分类名信息
      def show_category_name(self, obj):
          return obj.category.name
  
      show_category_name.short_description = '分类名'
  
  
  # 新增日志记录的管理链接
  @admin.register(LogEntry, site=custom_site)
  class LogEntryAdmin(admin.ModelAdmin):
      list_display = ['object_repr', 'object_id',
                      'action_flag', 'user', 'change_message']
  
  
  # Register your models here.
  