Django笔记11:注册admin

2021-08-19  本文已影响0人  _百草_
# app_label/admin.py

# -*- coding:utf-8 -*-
from django.contrib import admin
from app_0814.models import BlogArticles   # 导入类


# Register your models here.
class BlogArticlesAdmin(admin.ModelAdmin):  # 该类也需要注册到admin,否则无法生效
    list_display = ("title", "author", "publish")  # 添加列表中的字段
    list_filter = ("publish", "author")  # 快速过滤
    search_fields = ("title", "text")  # 搜索输入框,匹配字段
    raw_id_fields = ("author",)  # 针对外键添加外连接搜索
    list_per_page = 5  # 每页显示条目数
    # date_hierarchy = ("publish",)  # 日期月份的筛选 AttributeError: 'tuple' object has no attribute 'split'
    date_hierarchy = "publish"  # 添加日期月份的筛选,注意是字符串而非元组
    ordering = ("publish",)  # 列表排序规则,字段名前添加-,则表示倒序

    # list_editable = ("title",)  # 设置可编辑字段
    # <class 'app_0814.admin.BlogArticlesAdmin'>: (admin.E124)
    # The value of 'list_editable[0]' refers to the first field in 'list_display' ('title'),
    # which cannot be used unless 'list_display_links' is set.
    #
    list_editable = ("publish",)  # 列表页,批量编辑,需要在list_display中,且不能是第一个
    # <class 'app_0814.admin.BlogArticlesAdmin'>: (admin.E122)
    # The value of 'list_editable[0]' refers to 'text', which is not contained in 'list_display'.


admin.site.register(BlogArticles, BlogArticlesAdmin)  # 注册到admin

上一篇下一篇

猜你喜欢

热点阅读