码迷,mamicode.com
首页 > 其他好文 > 详细

Django基础,Day8 - 管理后台定制显示

时间:2017-01-07 07:57:21      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:form   port   cte   显示   size   tac   indicator   images   closed   

自定义admin表单

展示效果一:

技术分享

技术分享
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

展示效果二:

技术分享

技术分享
 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添加界面显示如:

技术分享

其二,在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‘)

增加过滤器    list_filter = [‘pub_date‘]

添加搜索栏    search_fields = [‘question_text‘]

技术分享

技术分享
 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

 

Django基础,Day8 - 管理后台定制显示

标签:form   port   cte   显示   size   tac   indicator   images   closed   

原文地址:http://www.cnblogs.com/guanfuchang/p/6258173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!