Django基础,Day8 - 管理后台定制显示
Posted 鲨鱼逛大街
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django基础,Day8 - 管理后台定制显示相关的知识,希望对你有一定的参考价值。
自定义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)
展示效果二:
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)
添加关联对象
一个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)
自定义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)
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
以上是关于Django基础,Day8 - 管理后台定制显示的主要内容,如果未能解决你的问题,请参考以下文章