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)
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

 


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

以上是关于Django基础,Day8 - 管理后台定制显示的主要内容,如果未能解决你的问题,请参考以下文章

Django开发博客系统(07-根据需求定制管理后台)

Django-----定制后台以及修改模型

软件系统定制定做学生管理系统软件定制开发代做学生管理软件系统管理软件定制开发

django admin实现后台多用户隔离

Python学习day8--linux基础

django-17.admin后台管理