首页 > 其他 > 详细

django admin 扩展

时间:2014-09-16 21:56:01      阅读:384      评论:0      收藏:0      [点我收藏+]

添加自定义动作:

例子,添加一个方法,批量更新文章,代码如下:

from django.contrib import admin
from myapp.models import Article

def make_published(self, request, queryset):
    rows_updated = queryset.update(status=’p’)
    if rows_updated == 1:
        message_bit = "1 story was"
    else:
        message_bit = "%s stories were" % rows_updated
    self.message_user(request, "%s successfully marked as published." % message_bit)

class ArticleAdmin(admin.ModelAdmin):
    list_display = [’title’, ’status’]
    ordering = [’title’]
    actions = [make_published]

admin.site.register(Article, ArticleAdmin)

  

效果如下:

bubuko.com,布布扣

自定义动作的权限控制,重载get_actions方法,如下:(只允许名字以J大头的删除记录)

from django.contrib import admin
from myapp.models import Article

def make_published(self, request, queryset):
    rows_updated = queryset.update(status=’p’)
    if rows_updated == 1:
        message_bit = "1 story was"
    else:
        message_bit = "%s stories were" % rows_updated
    self.message_user(request, "%s successfully marked as published." % message_bit)

class ArticleAdmin(admin.ModelAdmin):
    list_display = [’title’, ’status’]
    ordering = [’title’]
    actions = [make_published]
    
    def get_actions(self, request):
        actions = super(MyModelAdmin, self).get_actions(request)
        if request.user.username[0].upper() != ’J’:
            if ’delete_selected’ in actions:
                del actions[’delete_selected’]
        return actions

  

django admin 扩展

原文:http://www.cnblogs.com/Tommy-Yu/p/3975830.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!