01.Django Admin基本使用
1.1 models.py创建表结构
from django.db import models
class User(models.Model):
username = models.CharField(max_length=32,verbose_name='用户名')
pwd = models.CharField(max_length=64,verbose_name='密码')
ctime = models.DateTimeField(auto_now=True)
ut = models.ForeignKey(to='UserType',to_field='id',verbose_name='用户类型')
m2m = models.ManyToManyField("Group")
def __str__(self):
return self.username
class Meta:
verbose_name_plural = '用户表'
class UserType(models.Model):
user_type_name = models.CharField(max_length=32)
def __str__(self):
return self.user_type_name
class Group(models.Model):
group_name = models.CharField(max_length=32)
def __str__(self):
return self.group_name
1.2 admin.py中注册表
from django.contrib import admin
from app01 import models
class UserAdmin(admin.ModelAdmin):
list_display = ('username','pwd','ut','ctime',)
admin.site.register(models.User,UserAdmin)
admin.site.register(models.UserType)
admin.site.register(models.Group)
02.Admin自定制常用参数
from django.contrib import admin
from app01 import models
class UserAdmin(admin.ModelAdmin):
list_display = ('username','pwd','ut','ctime',)
list_filter = ('source','consultant','date') #过滤字段
search_fields = ('qq','name') #搜索匹配字段
raw_id_fields = ('consult_course',)
filter_horizontal = ('tags',) #多对多字段显示
list_per_page = 1 #每页显示几条数据
list_editable = ('source',) #可编辑的字段
readonly_fields = ('qq',) #只读字段
exclude = ('name',) # 添加和修改时那些界面不显示
date_hierarchy = 'ctime' # 详细时间分层筛选
actions = ['test_action',] #之定义的action函数
def test_action(self, request, arg2): # 自定义action函数
'''
:param self: crm.CustomerAdmin类本身
:param request: 客户端request请求
:param arg2: 前端选中的数据实例
'''
admin.site.register(models.User,UserAdmin)
admin.site.site_header = '重写DjangoAdmin管理系统' # 修改系统显示名称
admin.site.site_title = '我的后台管理界面' # 修改页面 title