Django_Admin
创建Django_Admin 管理账户
D:\github\Django_Aadmin>python manage.py createsuperuser
Username (leave blank to use 'dpad'): root
Email address:
Password: # 密码有复杂度要求
Password (again):
Superuser created successfully.
添加表到 Django_Admin 中进行管理
from django.contrib import admin
from app01 import models
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)
设置显示为 正常名而非 UserInfo object
1.在创建表的时候,添加__str__(self)
class UserInfo(models.Model):
"""
用户表
"""
username = models.CharField(max_length=32)
email = models.EmailField(max_length=32)
dp = models.ForeignKey(to="Department")
def __str__(self):
return self.username
定制当前列表页面,显示哪几列数据admin.py 文件中
from django.contrib import admin
from app01 import models
# 方法一
class UserInfoConfig(admin.ModelAdmin):
# 定制当前列表页面,显示哪几列数据
list_display = ["username", "email", "dp"]
# list_display_links = ["email"] # 设置 可点击 <a> 标签在哪个 字段上
# 继承定义的 UserInfoConfig 类
admin.site.register(models.UserInfo, UserInfoConfig)
# 方法二
@admin.register(models.UserInfo) # 通过装饰器实现
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp") # 第一个参数可以是列表
自定义编辑列
1.定义编辑列的函数值
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
自定义分组功能
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
# ##分组查询 过滤器
list_filter = ["dp"]
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)
自定义指定的列,处于可修改状态
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
list_filter = ["dp"]
# 时刻处于可修改状态,但该列不能处于可点击<a> 标签状态
list_editable = ('username',)
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)
自定义指定的列,可用于模糊查询(出现搜索框)
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
list_editable = ('username',)
list_filter = ["dp"]
# 可用于模糊查询的列
search_fields = ["username", "email",]
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)
定义编辑页面 上下都有可点击保存按钮
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
list_filter = ["dp"]
search_fields = ["username", "email",]
# 定义编辑页面 上下都有可点击保存按钮
save_on_top = True
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)
定义选中动作,批量执行操作
from django.contrib import admin
from app01 import models
@admin.register(models.UserInfo)
class UserInfoConfig(admin.ModelAdmin):
list_display = ("username", "email", "dp", "edit", "remove")
list_display_links = ["edit", "remove"]
list_filter = ["dp"]
search_fields = ["username", "email",]
save_on_top = True
# fields = ('username',"email")
def edit(self, obj):
return "编辑"
def remove(self, obj):
return "删除"
# 定制Action行为具体方法
def func(self, request, queryset):
print(self, request, queryset)
print("可以拿到选中的 ID 值:", request.POST.getlist('_selected_action'))
id_list = request.POST.getlist('_selected_action')
# 数据库操作,删除选中的 id 数据
# models.UserInfo.objects.filter(id__in=id_list).delete()
func.short_description = "自定义删除选中"
actions = [func, ] # 会触发执行 func 函数
# 添加 Department 表到 Django_Admin 管理
admin.site.register(models.Department)