Q: xadmin列表页面, 多选记录, 进行操作(标记为订阅, 未订阅)
A:
删除是一个Action(特殊的plugin), 所以可以仿照着删除写Action
actions.py
class YaraRulesetLookatAction(BaseActionView):
action_name = "lookat_yararuleset"
description = '订阅 %(verbose_name_plural)s'
model_perm = 'change' # 该 Action 所需权限
icon = 'fa fa-bookmark'
# 执行的动作
def do_action(self, queryset):
for obj in queryset:
# 需执行model对应的字段
if obj.lookup_users:
if self.request.user.username not in obj.lookup_users:
obj.lookup_users.append(self.request.user.username)
else:
obj.lookup_users = [self.request.user.username]
obj.save()
# return HttpResponse
return None # 返回的url地址
class YaraRulesetNoLookatAction(BaseActionView):
action_name = "nolookat_yararuleset"
description = '取消订阅 %(verbose_name_plural)s'
model_perm = 'change' # 该 Action 所需权限
icon = 'fa fa-bookmark'
# 执行的动作
def do_action(self, queryset):
for obj in queryset:
# 需执行model对应的字段
if obj.lookup_users:
if self.request.user.username in obj.lookup_users:
obj.lookup_users.remove(self.request.user.username)
obj.save()
# return HttpResponse
return None # 返回的url地址
adminx.py
from .actions import YaraRulesetLookatAction, YaraRulesetNoLookatAction
class YaraRulesetAdmin:
actions = [YaraRulesetLookatAction, YaraRulesetNoLookatAction]
效果图:
###