您的位置:首页 > 编程语言 > Go语言

django进阶-modelform&admin action

2017-03-22 17:52 447 查看

先看效果图:

登陆admin后的界面:

from django.contrib import admin

# Register your models here.
from app01 import models

def make_forbidden(modelAdmin, request, queryset):  #queryset:选中的集合;modelAdmin代表BookAdmin类,相当于self
print("----->", modelAdmin,request,queryset)
queryset.update(status="forbidden")  #更改选中的为禁书
#使action框有选项set to forbidden
make_forbidden.short_description = "Set to forbidden"

class BookAdmin(admin.ModelAdmin):  #定制book,需将类当作参数传给admin
# list_display不能显示多对多的,比如authors:
# (admin.E109) The value of 'list_display[2]' must not be a ManyToManyField.
list_display = ("id","name", "publisher","publish_date","colored_status","status")  #定制为三列

search_fields = ("name", "publisher__name")   #__表示出版社关联到名字
list_filter = ("publisher", "publish_date")  #过滤
list_editable = ("name", "publish_date")     #可修改
list_per_page = 10   #每页显示数量
#即便只有一个值也要加逗号,不然就不当成元组了
#The value of 'filter_horizontal' must be a list or tuple.
filter_horizontal = ("authors",)   #选择作者时进行定制 PS:用于多对多关联
raw_id_fields = ("publisher",)    #选择出版社时进行定制 PS:用于FK外键关联

actions = [make_forbidden]

admin.site.register(models.Author)
admin.site.register(models.Book, BookAdmin)
admin.site.register(models.Publisher)


View Code

可别小看admin action, 比如你可以批量选中很多主机,点击action发送指令,发送文件 and so on. 强!!!

转发注明出处: http://www.cnblogs.com/0zcl/p/6580279.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: