zoukankan      html  css  js  c++  java
  • xadmin 自定义actions

    1.编写actions

    from xadmin.plugins.actions import BaseActionView
    from django.contrib import messages
    
    class ClaimAction(BaseActionView):
        '''
        批量认领功能
        '''
        # 这里需要填写三个属性
        action_name = "claim"  #: 相当于这个 Action 的唯一标示, 尽量用比较针对性的名字
        description = _(u'收养选中的商品')  #: 描述, 出现在 Action 菜单中, 可以使用 ``%(verbose_name_plural)s`` 代替 Model 的名字.
        icon = 'fa fa-tasks'  # 图标
        model_perm = 'change'  #: 该 Action 所需权限
    
        # 而后实现 do_action 方法
        def do_action(self, queryset):
            # queryset 是包含了已经选择的数据的 queryset
            queryset.update(belonger=self.request.user)
    
            messages.success(self.request, '认领成功')
            # 返回 HttpResponse
            return redirect(self.request.get_full_path())

    2.引用

    from erp.xadminconf.myaction import ClaimAction
    
    class ClaimGoodsAdmin(GoodsInfoModelAdmin):
        # reversion_enable = True
        actions = (ClaimAction,)

    3.xadmin会默认引用父类的actions,如果不需要可以通过修改源码解决

    # 路径 xadminpluginsactions.py
    
    # 大概在236行
        def get_actions(self):
            if self.actions is None:
                return OrderedDict()
    
            actions = [self.get_action(action) for action in self.global_actions]
            
            # for klass in self.admin_view.__class__.mro()[::-1]: # 注释掉这一句
            for klass in (self.admin_view,): # 改成这一句
                class_actions = getattr(klass, 'actions', [])
    
                if not class_actions:
                    continue
                actions.extend(
                    [self.get_action(action) for action in class_actions])
    
            # get_action might have returned None, so filter any of those out.
            actions = filter(None, actions)
            if six.PY3:
                actions = list(actions)
    
            # Convert the actions into a OrderedDict keyed by name.
            actions = OrderedDict([
                (name, (ac, name, desc, icon))
                for ac, name, desc, icon in actions
            ])
    
            return actions
  • 相关阅读:
    修理牛棚 贪心 USACO
    零件加工 贪心 题解
    花店橱窗 动态规划 题解
    动态规划 摆花 题解
    NOIP2004普及组第3题 FBI树
    实况世界杯4小游戏链接
    poj2761(treap入门)
    最大连续子序列和(分治法)
    任意区间的最长连续递增子序列,最大连续子序列和
    lca转RMQ
  • 原文地址:https://www.cnblogs.com/wtil/p/12627561.html
Copyright © 2011-2022 走看看