Django框架admin

摘要

django amdin是django提供的一个后台管理页面,改管理页面提供完善的html和css,使得你在通过Model创建完数据库表之后,就可以对数据进行增删改查,而使用django admin 则需要以下步骤:

  • 创建后台管理员
  • 配置url
  • 注册和配置django admin后台管理页面

Admin

Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有:

1
2
3
4
5
6
7
8
9
10
11
12
13
依赖APP:
django.contrib.auth
django.contrib.contenttypes
django.contrib.messages
django.contrib.sessions

模板的context_processors:
django.contrib.auth.context_processors.auth
django.contrib.messages.context_processors.messages

中间件:
django.contrib.auth.middleware.AuthenticationMiddleware
django.contrib.messages.middleware.MessageMiddleware

创建后台管理员

1
python manage.py createsuperuser

配置后台管理url

1
url(r'^admin/', include(admin.site.urls))

注册和配置django admin后台管理页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.contrib import admin
from app01 import models

# admin后台数据管理界面定制
class AppAdmin(admin.ModelAdmin):
# 页面默认显示的字段
list_display = ('name', 'description', 'config_path', 'md5', 'status')
# 为数据表添加搜索功能
search_fields = ('name',)
# 添加快速过滤
list_filter = ('name', 'status')
# 排序字段
ordering = ('name',)
# 可编辑的字段名
fields = ('name', 'description', 'config_path', 'md5', 'status')
# many to many 字段多选
# filter_horizontal = ('url',)
date_hierarchy = 'create_at'

admin.site.register(models.App, AppAdmin)

定制Admin

1
2
3
4
5
6
7
8
9
10
11
12
13
# 在admin.py中只需要讲Mode中的某个类注册,即可在Admin中实现增删改查的功能,如:
admin.site.register(models.UserInfo)

# 如果想要进行更多的定制操作,需要利用ModelAdmin进行操作
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd',)
# 第一个参数可以是列表
admin.site.register(models.UserInfo, UserAdmin)

# 第一个参数可以是列表
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd',)

list_display

列表时,定制显示的列

1
2
3
4
5
6
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd', 'xxxxx')

def xxxxx(self, obj):
return "xxxxx"

列表时,定制列可以点击跳转。

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd', 'xxxxx')
list_display_links = ('pwd',)

list_filter

列表时,定制右侧快速筛选。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.utils.translation import ugettext_lazy as _

@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd')

class Ugg(admin.SimpleListFilter):
title = _('decade born')
parameter_name = 'xxxxxx'

def lookups(self, request, model_admin):
"""
显示筛选选项
:param request:
:param model_admin:
:return:
"""
return models.UserGroup.objects.values_list('id', 'title')

def queryset(self, request, queryset):
"""
点击查询时,进行筛选
:param request:
:param queryset:
:return:
"""
v = self.value()
return queryset.filter(ug=v)

list_filter = ('user',Ugg,)

列表时,连表查询是否自动select_related

分页相关

1
2
3
4
5
6
7
8
# 分页,每页显示条数
list_per_page = 100

# 分页,显示全部(真实数据<该值时,才会有显示全部)
list_max_show_all = 200

# 分页插件
paginator = Paginator

list_editable

列表时,可以编辑的列

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
list_display = ('user', 'pwd','ug',)
list_editable = ('ug',)

search_fields

列表时,模糊搜索的功能

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

search_fields = ('user', 'pwd')

date_hierarchy

列表时,对Date和DateTime类型进行搜索

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

date_hierarchy = 'ctime'

preserve_filters

详细页面,删除、修改,更新后跳转回列表后,是否保留原搜索条件

save_as = False

详细页面,按钮为“Sava as new” 或 “Sava and add another”

save_as_continue = True

点击保存并继续编辑

1
2
3
4
5
6
save_as_continue = True

# 如果 save_as=True,save_as_continue = True, 点击Sava as new 按钮后继续编辑。
# 如果 save_as=True,save_as_continue = False,点击Sava as new 按钮后返回列表。

New in Django 1.10.

save_on_top = False

详细页面,在页面上方是否也显示保存删除等按钮

inlines

详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除

1
2
3
4
5
6
7
8
class UserInfoInline(admin.StackedInline): # TabularInline
extra = 0
model = models.UserInfo


class GroupAdminMode(admin.ModelAdmin):
list_display = ('id', 'title',)
inlines = [UserInfoInline, ]

action

列表时,定制action中的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

# 定制Action行为具体方法
def func(self, request, queryset):
print(self, request, queryset)
print(request.POST.getlist('_selected_action'))

func.short_description = "中文显示自定义Actions"
actions = [func, ]

# Action选项都是在页面上方显示
actions_on_top = True
# Action选项都是在页面下方显示
actions_on_bottom = False

# 是否显示选择个数
actions_selection_counter = True

定制HTML模板

1
2
3
4
5
6
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

raw_id_fields

详细页面,针对FK和M2M字段变成以Input框形式

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

raw_id_fields = ('FK字段', 'M2M字段',)

fields

详细页面时,显示字段的字段

1
2
3
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
fields = ('user',)

exclude

详细页面时,排除的字段

1
2
3
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
exclude = ('user',)

readonly_fields

详细页面时,只读字段

1
2
3
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
readonly_fields = ('user',)

fieldsets

详细页面时,使用fieldsets标签对数据进行分割显示

1
2
3
4
5
6
7
8
9
10
11
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
fieldsets = (
('基本数据', {
'fields': ('user', 'pwd', 'ctime',)
}),
('其他', {
'classes': ('collapse', 'wide', 'extrapretty'), # 'collapse','wide', 'extrapretty'
'fields': ('user', 'pwd'),
}),
)

详细页面时,M2M显示时

数据移动选择(方向:上下和左右)

1
2
3
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)

ordering

列表时,数据排序规则

1
2
3
4
5
6
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
ordering = ('-id',)

def get_ordering(self, request):
return ['-id', ]

view_on_site

编辑时,是否在页面上显示view on set

1
2
3
4
view_on_site = False

def view_on_site(self, obj):
return 'https://www.baidu.com'

radio_fields

详细页面时,使用radio显示选项(FK默认使用select)

1
radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL

show_full_result_count = True

列表时,模糊搜索后面显示的数据个数样式

1
2
3
4
5
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):
# show_full_result_count = True # 1 result (12 total)
# show_full_result_count = False # 1 result (Show all)
search_fields = ('user',)

formfield_overrides = {}

详细页面时,指定显示插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.forms import widgets
from django.utils.html import format_html

class MyTextarea(widgets.Widget):
def __init__(self, attrs=None):
# Use slightly better defaults than HTML's 20x2 box
default_attrs = {'cols': '40', 'rows': '10'}
if attrs:
default_attrs.update(attrs)
super(MyTextarea, self).__init__(default_attrs)

def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, name=name)
return format_html('<textarea {}>\r\n{}</textarea>',final_attrs, value)



@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

formfield_overrides = {
models.models.CharField: {'widget': MyTextarea},
}

prepopulated_fields = {}

添加页面,当在某字段填入值后,自动会将值填充到指定字段

1
2
3
4
@admin.register(models.UserInfo)
class UserAdmin(admin.ModelAdmin):

prepopulated_fields = {"email": ("user","pwd",)}

DjangoAdmin中使用js实现功能,页面email字段的值会在输入:user、pwd时自动填充

form = ModelForm

用于定制用户请求时候表单验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

empty_value_display = ""

列数据为空时,显示默认值

1
2
3
4
5
6
7
8
9
@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 = "指定列数据为空时,默认显示"