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