Django基礎,Day8 - 管理後臺定製顯示

自定義admin表單

展現效果一:python

1 from django.contrib import admin
2 from polls.models import Question
3 
4 class QuestionAdmin(admin.ModelAdmin):
5     fields = ['pub_date', 'question_text']
6 
7 admin.site.register(Question, QuestionAdmin)
polls/admin.py

展現效果二:django

 1 from django.contrib import admin
 2 from polls.models import Question
 3 
 4 class QuestionAdmin(admin.ModelAdmin):
 5     fieldsets = [
 6         (None,               {'fields': ['question_text']}),
 7         ('Date information', {'fields': ['pub_date']}),
 8     ]
 9 
10 admin.site.register(Question, QuestionAdmin)
polls/admin.py

 添加關聯對象

一個Question有多個Choice,Choice的顯示方式有兩種:微信

其一,直接註冊 admin.site.register(Choice),則Choice添加界面顯示如:ide

其二,在Question中關聯Choice,效果展現如:測試

 1 from django.contrib import admin
 2 from polls.models import Question, Choice
 3 
 4 
 5 class ChoiceInline(admin.TabularInline): # admin.TabularInline admin.StackedInline
 6     model = Choice
 7     extra = 0
 8 
 9 
10 class QuestionAdmin(admin.ModelAdmin):
11     fieldsets = [
12         (None,               {'fields': ['question_text']}),
13         ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
14     ]
15     inlines = [ChoiceInline]
16 
17 admin.site.register(Question, QuestionAdmin)
polls/admin.py

 自定義admin列表

增長顯示列    list_display = ('question_text', 'pub_date', 'was_published_recently')spa

增長過濾器    list_filter = ['pub_date']code

添加搜索欄    search_fields = ['question_text']orm

 1 from django.contrib import admin
 2 from polls.models import Question, Choice
 3 
 4 
 5 class ChoiceInline(admin.TabularInline): # admin.TabularInline admin.StackedInline
 6     model = Choice
 7     extra = 0
 8 
 9 
10 class QuestionAdmin(admin.ModelAdmin):
11     fieldsets = [
12         (None,               {'fields': ['question_text']}),
13         ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
14     ]
15     inlines = [ChoiceInline]
16     list_display = ('question_text', 'pub_date', 'was_published_recently')
17     list_filter = ['pub_date']
18     search_fields = ['question_text']
19 admin.site.register(Question, QuestionAdmin)
polls/admin.py

 


***微信掃一掃,關注「python測試開發圈」,瞭解更多測試教程!***
相關文章
相關標籤/搜索