django 內置 Adminhtml
Django內置的Admin是對於model中對應的數據表進行增刪改查提供的組件,使用方式有:python
1 依賴APP: 2 django.contrib.auth 3 django.contrib.contenttypes 4 django.contrib.messages 5 django.contrib.sessions 6 7 模板的context_processors: 8 django.contrib.auth.context_processors.auth 9 django.contrib.messages.context_processors.messages 10 11 中間件: 12 django.contrib.auth.middleware.AuthenticationMiddleware 13 django.contrib.messages.middleware.MessageMiddleware 14 15 Django Admin內部依賴
urlpatterns
=
[
django
url(r
'^admin/'
, admin.site.urls),
]
在admin.py中只須要講Mode中的某個類註冊,便可在Admin中實現增刪改查的功能,如:session
1 admin.site.register(models.UserInfo)
可是,這種方式比較簡單,若是想要進行更多的定製操做,須要利用ModelAdmin進行操做,如:app
方式一: 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',)
ModelAdmin中提供了大量的可定製功能,如ide
1. list_display,列表時,定製顯示的列ui
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 list_display = ('user', 'pwd', 'xxxxx') 4 5 def xxxxx(self, obj): 6 return "xxxxx"
2. list_display_links,列表時,定製列能夠點擊跳轉。url
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 list_display = ('user', 'pwd', 'xxxxx') 4 list_display_links = ('pwd',)
3. list_filter,列表時,定製右側快速篩選spa
1 from django.utils.translation import ugettext_lazy as _ 2 3 @admin.register(models.UserInfo) 4 class UserAdmin(admin.ModelAdmin): 5 list_display = ('user', 'pwd') 6 7 class Ugg(admin.SimpleListFilter): 8 title = _('decade born') 9 parameter_name = 'xxxxxx' 10 11 def lookups(self, request, model_admin): 12 """ 13 顯示篩選選項 14 :param request: 15 :param model_admin: 16 :return: 17 """ 18 return models.UserGroup.objects.values_list('id', 'title') 19 20 def queryset(self, request, queryset): 21 """ 22 點擊查詢時,進行篩選 23 :param request: 24 :param queryset: 25 :return: 26 """ 27 v = self.value() 28 return queryset.filter(ug=v) 29 30 list_filter = ('user',Ugg,)
4. list_select_related,列表時,連表查詢是否自動select_related插件
5. 分頁相關
1 # 分頁,每頁顯示條數 2 list_per_page = 100 3 4 # 分頁,顯示所有(真實數據<該值時,纔會有顯示所有) 5 list_max_show_all = 200 6 7 # 分頁插件 8 paginator = Paginator
6. list_editable,列表時,能夠編輯的列
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 list_display = ('user', 'pwd','ug',) 4 list_editable = ('ug',)
7. search_fields,列表時,模糊搜索的功能
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 4 search_fields = ('user', 'pwd')
8. date_hierarchy,列表時,對Date和DateTime類型進行搜索
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 4 date_hierarchy = 'ctime'
9. preserve_filters,詳細頁面,刪除、修改,更新後跳轉回列表後,是否保留原搜索條件
10. save_as = False,詳細頁面,按鈕爲「Sava as new」 或 「Sava and add another」
11. save_as_continue = True,點擊保存並繼續編輯
1 save_as_continue = True 2 3 # 若是 save_as=True,save_as_continue = True, 點擊Sava as new 按鈕後繼續編輯。 4 # 若是 save_as=True,save_as_continue = False,點擊Sava as new 按鈕後返回列表。 5 6 New in Django 1.10.
12. save_on_top = False,詳細頁面,在頁面上方是否也顯示保存刪除等按鈕
13. inlines,詳細頁面,若是有其餘表和當前表作FK,那麼詳細頁面能夠進行動態增長和刪除
1 class UserInfoInline(admin.StackedInline): # TabularInline 2 extra = 0 3 model = models.UserInfo 4 5 6 class GroupAdminMode(admin.ModelAdmin): 7 list_display = ('id', 'title',) 8 inlines = [UserInfoInline, ]
14. action,列表時,定製action中的操做
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 4 # 定製Action行爲具體方法 5 def func(self, request, queryset): 6 print(self, request, queryset) 7 print(request.POST.getlist('_selected_action')) 8 9 func.short_description = "中文顯示自定義Actions" 10 actions = [func, ] 11 12 # Action選項都是在頁面上方顯示 13 actions_on_top = True 14 # Action選項都是在頁面下方顯示 15 actions_on_bottom = False 16 17 # 是否顯示選擇個數 18 actions_selection_counter = True
15. 定製HTML模板
1 add_form_template = None 2 change_form_template = None 3 change_list_template = None 4 delete_confirmation_template = None 5 delete_selected_confirmation_template = None 6 object_history_template = None
16. raw_id_fields,詳細頁面,針對FK和M2M字段變成以Input框形式
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 4 raw_id_fields = ('FK字段', 'M2M字段',)
17. fields,詳細頁面時,顯示字段的字段
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 fields = ('user',)
18. exclude,詳細頁面時,排除的字段
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 exclude = ('user',)
19. readonly_fields,詳細頁面時,只讀字段
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 readonly_fields = ('user',)
20. fieldsets,詳細頁面時,使用fieldsets標籤對數據進行分割顯示
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 fieldsets = ( 4 ('基本數據', { 5 'fields': ('user', 'pwd', 'ctime',) 6 }), 7 ('其餘', { 8 'classes': ('collapse', 'wide', 'extrapretty'), # 'collapse','wide', 'extrapretty' 9 'fields': ('user', 'pwd'), 10 }), 11 )
21. 詳細頁面時,M2M顯示時,數據移動選擇(方向:上下和左右)
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 filter_vertical = ("m2m字段",) # 或filter_horizontal = ("m2m字段",)
22. ordering,列表時,數據排序規則
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 ordering = ('-id',) 4 或 5 def get_ordering(self, request): 6 return ['-id', ]
23. view_on_site,編輯時,是否在頁面上顯示view on set
1 view_on_site = False 2 或 3 def view_on_site(self, obj): 4 return 'https://www.baidu.com'
24. radio_fields,詳細頁面時,使用radio顯示選項(FK默認使用select)
radio_fields = {"ug": admin.VERTICAL} # 或admin.HORIZONTAL
25. show_full_result_count = True,列表時,模糊搜索後面顯示的數據個數樣式
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 # show_full_result_count = True # 1 result (12 total) 4 # show_full_result_count = False # 1 result (Show all) 5 search_fields = ('user',)
26. formfield_overrides = {},詳細頁面時,指定現實插件
1 from django.forms import widgets 2 from django.utils.html import format_html 3 4 class MyTextarea(widgets.Widget): 5 def __init__(self, attrs=None): 6 # Use slightly better defaults than HTML's 20x2 box 7 default_attrs = {'cols': '40', 'rows': '10'} 8 if attrs: 9 default_attrs.update(attrs) 10 super(MyTextarea, self).__init__(default_attrs) 11 12 def render(self, name, value, attrs=None): 13 if value is None: 14 value = '' 15 final_attrs = self.build_attrs(attrs, name=name) 16 return format_html('<textarea {}>\r\n{}</textarea>',final_attrs, value) 17 18 19 20 @admin.register(models.UserInfo) 21 class UserAdmin(admin.ModelAdmin): 22 23 formfield_overrides = { 24 models.models.CharField: {'widget': MyTextarea}, 25 }
27. prepopulated_fields = {},添加頁面,當在某字段填入值後,自動會將值填充到指定字段。
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 4 prepopulated_fields = {"email": ("user","pwd",)}
28. form = ModelForm,用於定製用戶請求時候表單驗證
1 rom app01 import models 2 from django.forms import ModelForm 3 from django.forms import fields 4 5 6 class MyForm(ModelForm): 7 others = fields.CharField() 8 9 class Meta: 10 model = models = models.UserInfo 11 fields = "__all__" 12 13 @admin.register(models.UserInfo) 14 class UserAdmin(admin.ModelAdmin): 15 16 form = MyForm
29. empty_value_display = "列數據爲空時,顯示默認值"
1 @admin.register(models.UserInfo) 2 class UserAdmin(admin.ModelAdmin): 3 empty_value_display = "列數據爲空時,默認顯示" 4 5 list_display = ('user','pwd','up') 6 7 def up(self,obj): 8 return obj.user 9 up.empty_value_display = "指定列數據爲空時,默認顯示"