Django中Admin样式定制
Django自带的admin在展示数据是样式有点单一,我们可以自己定义数据的展示样式。
一、自定义数据展示样式
1、后台查询书记列表时,同时列出出版社和出版时间:
admin.py文件
from django.contrib import admin import models class BookAdmin(admin.ModelAdmin): list_display = ('name','publisher','publication_date') #展示 admin.site.register(models.Author) admin.site.register(models.Publisher) admin.site.register(models.Book,BookAdmin)
这样,展示效果如下图:
2、添加搜索框
admin.py
from django.contrib import admin import models class BookAdmin(admin.ModelAdmin): list_display = ('name','publisher','publication_date') search_fields = ('name','publisher__name') #搜索,'__'表示需要关连到其他表中查询 admin.site.register(models.Author) admin.site.register(models.Publisher) admin.site.register(models.Book,BookAdmin)
这样,就有搜索框了:
3、添加过滤器
admin.py
from django.contrib import admin import models class BookAdmin(admin.ModelAdmin): list_display = ('name','publisher','publication_date') search_fields = ('name','publisher__name') list_filter = ('name','publisher','publication_date') #过滤 admin.site.register(models.Author) admin.site.register(models.Publisher) admin.site.register(models.Book,BookAdmin)
这样,在admin页面的右侧就添加了一个过滤器
4、编辑每行字段
admin.py
from django.contrib import admin import models class BookAdmin(admin.ModelAdmin): list_display = ('id','name','publisher','publication_date') search_fields = ('name','publisher__name') list_filter = ('name','publisher','publication_date') list_editable = ('name','publisher','publication_date') #可编辑的字段,注意,默认第一个字段不可编辑,因此添加'id'为第一个字段 admin.site.register(models.Author) admin.site.register(models.Publisher) admin.site.register(models.Book,BookAdmin)
效果如下:
5、设置每页显示的条数
admin.py
BookAdmin类中添加
list_per_page = 10
6、如果选项太多,可以先搜索,然后再添加
admin.py
filter_horizontal = ('authors',)
现实效果为:
二、Admin中Actions
给book新添加一个状态,表示出版和未出版,让其显示在admin里书籍的页面中。
1、在models中book类下添加status字段
class Book(models.Model): name = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() status_choices=( ('published','已出版'), ('publishing','未出版'), ('forbidden','禁止出版'), ) status = models.CharField(choices=status_choices,max_length=32,default='publishing') #default字段必须添加,否则同步数据库会出错,默认为未出版 def __unicode__(self): return self.name class Meta: verbose_name_plural='书籍名称'
2、同步数据库
sudo python manage.py makemigrations
sudo python manage.py migrate
然后在amdin中book信息显示叶就多了status的选项:
3、显示数据信息时,将状态显示出来
admin.py
class BookAdmin(admin.ModelAdmin): list_display = ('id','name','publisher','publication_date','status')
默认每本书的状态都是'未出版',如果现在要批量修改为'已出版'或者'禁止出版',就要使用action了。
在actions中添加已出版和禁止出版选项,然后不同的状态显示不同的背景颜色。
models.py
#!_*_ coding:utf-8 _*_
from __future__ import unicode_literals
from django.db import models
from django.utils.html import format_html #format_html是将html代码传给admin时依html格式显示,否则只显示本身字符串
class Book(models.Model): name = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() status_choices=( ('published',u'已出版'), ('publishing',u'未出版'), ('forbidden',u'禁止出版'), ) status = models.CharField(choices=status_choices,max_length=32,default='publishing') def __unicode__(self): return self.name class Meta: # 显示中文 verbose_name_plural=u'书籍名称' def status_color(self): if self.status=='published': format_td=format_html('<span style="padding:2px;background-color:yellowgreen;color:white">已出版</span>') if self.status=='publishing': format_td=format_html('<span style="padding:2px;background-color:pink;color:white">未出版</span>') if self.status=='forbidden': format_td=format_html('<span style="padding:2px;background-color:orange;color:white">禁止出版</span>') return format_td #将html样式传递给admin展示页面 status_color.short_description='status' #admin展示时名称为status(一个别名)
admin.py
def make_published(modelAdmin,request,queryset): #queryset是选中的对象的集合 queryset.update(status='published') make_published.short_description='出版' def make_forbidden(modelAdmin,request,queryset): queryset.update(status='forbidden') make_forbidden.short_description='禁止出版' class BookAdmin(admin.ModelAdmin): list_display = ('id','name','publisher','publication_date','status_color') #添加model中新定义的函数status_color,在admin中显示出来 search_fields = ('name','publisher__name') list_filter = ('name','publisher','publication_date') list_editable = ('name','publisher','publication_date') filter_horizontal = ('authors',) actions = [make_published,make_forbidden] #action中显示的选项
效果如下:
这样就可以批量的修改了。