Django 中Admin站点的配置

  • 自定义管理界面
    实现关联,在创建班级的时候添加两个学生
# 定义关联,在创建班级的时候添加2个学生
class StudentsAdd(admin.TabularInline):
    model = Students
    extra = 2

# 在班级模型添加关联
    inlines = [StudentsAdd]

全部实现代码

from django.contrib import admin

from user.models import Grades,Students # 从模块引入模型


# 定义关联,在创建班级的时候添加2个学生
class StudentsAdd(admin.TabularInline):
    model = Students
    extra = 2 # 设置为2


class GradesAdmin(admin.ModelAdmin):
    # 列表页属性
    def name(self):
        return self.g_name

    def girl_num(self):
        return self.girl_num

    def boy_num(self):
        return self.boy_num

    def time(self):
        return self.create_date

    def delete(self):
        return self.isDelete

    name.short_description = '班级名'
    girl_num.short_description = '女生人数'
    boy_num.short_description = '男生人数'
    time.short_description = '创建时间'
    delete.short_description = '是否删除'
    inlines = [StudentsAdd]
    list_display = ['pk',name,girl_num,boy_num,time,delete]  # 用于显示标题为中文
    list_filter = ['g_name']
    search_fields = ['g_name']
    list_per_page = 2
    # # 添加修改页属性
    # fields = ['g_name', 'boy_num', 'girl_num', 'create_date', 'isDelete']
    fieldsets = [('num', {'fields': ['boy_num', 'girl_num']}),
                 ('base', {'fields': ['g_name', 'create_date', 'isDelete']})]


admin.site.register(Grades,GradesAdmin)


# 装饰器实现注册
@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
    def gender(self):
        if self.s_gender:
            return '男'
        else:
            return '女'

    def name(self):
        return self.s_name

    def age(self):
        return self.s_age

    def content(self):
        return self.contend

    def grade(self):
        return self.s_grade

    def delete(self):
        return self.isDelete

    gender.short_description = '性别'
    name.short_description = '姓名'
    age.short_description = '年龄'
    content.short_description = '描述'
    grade.short_description = '班级'
    delete.short_description = '是否删除'

    list_display = ['pk',name,age,gender,content,grade,delete]
    list_filter = ['s_grade']
    search_fields = ['s_name']
    list_per_page = 3
    fieldsets = [('基本信息',{'fields':['s_name','s_age','s_gender','s_grade']}),
                 ('拓展信息',{'fields':['contend','isDelete']})]

    # 执行动作的位置
    actions_on_bottom = True
    actions_on_top = False
# admin.site.register(Students,StudentsAdmin)

posted @ 2018-10-28 11:45  RideoCos  阅读(748)  评论(0编辑  收藏  举报