admin app
Django 提供了基于 web 的管理工具。
Django 自动管理工具是 django.contrib 的一部分。你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它:
settings.py中
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"app01"
]
django.contrib是一套庞大的功能集,它是Django基本代码的组成部分。
启用admin管理工具
要启动admin管理工具,只需要在urls.py中启用admin对应的路由配置项即可。
通常我们在使用PyCharm生成项目时会在 urls.py 中自动设置好。
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
使用admin管理工具
首先先启动我们的Django项目,然后在浏览器中访问 http://127.0.0.1:8000/admin/,就能看到登陆界面。(具体网址和端口根据自己环境输入)
使用 python manage.py createsuperuser
来创建管理员账号,来登录admin管理后台。
想要在admin 界面管理某个数据模型(model),我们需要先将该数据模型(model)注册到 admin。
admin.site.register(models.Publisher)
admin.site.register(models.Book)
admin.site.register(models.Author)
在admin.py中只要按照上面的方式将某个模型类(model class)注册,即可在Admin中实现该model对应的数据表增删改查的功能。
admin的定制
这种方式的注册比较简单,如果想要进行更多的定制操作,需要利用ModelAdmin进行操作,如:
# 注册方式1
class PublisherAdmin(admin.ModelAdmin):
list_display = ("name", "address")
admin.site.register(models.Publisher, PublisherAdmin)
# 注册方式2
@admin.register(models.Book)
class BookAdmin(admin.ModelAdmin):
list_display = ("title", "price", "publish_date", "publisher")
表结构
class api_detail(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=100)
project_id = models.BigIntegerField()
method = models.CharField(max_length=11, blank=True, null=True)
domain = models.CharField(max_length=128, blank=True, null=True)
path = models.TextField(blank=True, null=True)
remark = models.TextField(blank=True, null=True)
before_excs = models.TextField(blank=True, null=True)
req_header = models.TextField(blank=True, null=True)
req_query = models.TextField(blank=True, null=True)
req_body_data = models.TextField(blank=True, null=True)
req_body_json = models.TextField(blank=True, null=True)
req_body_type = models.CharField(max_length=11, blank=True, null=True)
req_extract = models.TextField(blank=True, null=True)
req_assert = models.TextField(blank=True, null=True)
debug_rsp = models.CharField(max_length=20, blank=True, null=True)
create_by = models.CharField(max_length=50, blank=True, null=True)
create_time = models.DateTimeField()
update_by = models.CharField(max_length=50, blank=True, null=True)
update_time = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'api_detail'
1. list_display,列表时,定制显示的列。
from django.contrib import admin
from tools import models
# Register your models here.
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method')
2. list_display_links,列表时,定制列可以点击跳转。
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method')
list_display_links = ('name','project_id') # 必须已经在 list_display中存在
3. list_filter,列表时,定制右侧快速筛选。
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method')
list_display_links = ('name','project_id') # 必须已经在 list_display中存在
list_filter = ('name','project_id')
4. list_select_related,列表时,连表查询是否自动select_related
5. list_editable,列表时,可以编辑的列
注意
:同一个字段不能和 list_display_links
一起用
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method')
list_display_links = ('name',) # 必须已经在 list_display中存在
list_filter = ('name','project_id')
list_editable = ('project_id', 'method')
# def xxxxx(self, obj):
# return "xxxxx"
6. search_fields,列表时,模糊搜索的功能
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method')
list_display_links = ('name',) # 必须已经在 list_display中存在
list_filter = ('name','project_id')
list_editable = ('project_id', 'method')
search_fields = ('name', 'project_id', 'method')
7. date_hierarchy,列表时,对Date和DateTime类型进行搜索
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method',"create_time")
list_display_links = ('name',) # 必须已经在 list_display中存在
list_filter = ('name','project_id')
list_editable = ('project_id', 'method')
search_fields = ('name', 'project_id', 'method')
date_hierarchy = 'create_time'
8. inlines,详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除
class UserInfoInline(admin.StackedInline): # TabularInline
extra = 0
model = models.UserInfo
class GroupAdminMode(admin.ModelAdmin):
list_display = ('id', 'title',)
inlines = [UserInfoInline, ]
9. action,列表时,定制action中的操作
@admin.register(models.api_detail)
class api_detail(admin.ModelAdmin):
list_display = ('name', 'project_id', 'method',"create_time")
list_display_links = ('name',) # 必须已经在 list_display中存在
list_filter = ('name','project_id')
list_editable = ('project_id', 'method')
search_fields = ('name', 'project_id', 'method')
date_hierarchy = 'create_time'
# 定制Action行为具体方法
def func(self, request, queryset):
print("-------------------")
print(self, request, queryset)
print(request.POST.getlist('_selected_action'))
for i in queryset:
print(i.domain)
func.short_description = "中文显示自定义Actions"
actions = [func, ]
# Action选项都是在页面上方显示
actions_on_top = True
# Action选项都是在页面下方显示
actions_on_bottom = False
# 是否显示选择个数
actions_selection_counter = True
10. 定制HTML模板
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None
11. raw_id_fields,详细页面,针对FK和M2M字段变成以Input框形式
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
raw_id_fields = ('FK字段', 'M2M字段',)
12. fields,详细页面时,显示字段的字段
加了这个的话,没有fields的,就都不显示了。
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
fields = ('user',)
13. exclude,详细页面时,排除的字段
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
exclude = ('user',)
14. readonly_fields,详细页面时,只读字段
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
readonly_fields = ('user',)
15. fieldsets,详细页面时,使用fieldsets标签对数据进行分割显示
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
fieldsets = (
('基本数据', {
'fields': ('user', 'pwd', 'ctime',)
}),
('其他', {
'classes': ('collapse', 'wide', 'extrapretty'), # 'collapse','wide', 'extrapretty'
'fields': ('user', 'pwd'),
}),
)
16. 详细页面时,M2M显示时,数据移动选择(方向:上下和左右)
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)
17. ordering,列表时,数据排序规则
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
ordering = ('-id',)
或
def get_ordering(self, request):
return ['-id', ]
18. radio_fields,详细页面时,使用radio显示选项(FK默认使用select)
radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL
19. form = ModelForm,用于定制用户请求时候表单验证
from app01 import models
from django.forms import ModelForm
from django.forms import fields
class MyForm(ModelForm):
others = fields.CharField()
class Meta:
model = models = models.UserInfo
fields = "__all__"
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
form = MyForm
20. empty_value_display = "列数据为空时,显示默认值"
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
empty_value_display = "列数据为空时,默认显示"
list_display = ('user','pwd','up')
def up(self,obj):
return obj.user
up.empty_value_display = "指定列数据为空时,默认显示"