标签:查询 mamicode 控制 name long 删除权限 lis lag back
1.设置一个代理模型
# 普通模型 class Goods(models.Model): pass # 代理模型 class ClaimGoods(Goods): class Meta: verbose_name = "代理模型" verbose_name_plural = verbose_name proxy = True # 设置为True 否则会重新注册一张数据表
2.注册模型
class ClaimGoodsAdmin(GoodsInfoModelAdmin): # reversion_enable = True actions = [ClaimAction, ] def get_list_queryset(self): queryset = super().get_list_queryset() queryset = queryset.filter(belonger__isnull=True) return self.level_queryset(queryset) def get_permission_codename(self, action, opts): """ Return the codename of the permission for the specified action. """ return ‘%s_%s‘ % (action, opts.model_name) def has_view_permission(self, obj=None): flag = super(ClaimGoodsAdmin, self).has_view_permission(obj) view_codename = self.get_permission_codename(‘view‘, self.opts) # 查看权限代码 change_codename = self.get_permission_codename(‘change‘, self.opts) # 修改权限代码 del_codename = self.get_permission_codename(‘delete‘, self.opts) # 删除权限代码 add_codename = self.get_permission_codename(‘add‘, self.opts) # 添加权限代码 print(view_codename, del_codename, add_codename, change_codename) return flag xadmin.site.register(ClaimGoods, ClaimGoodsAdmin)
2.因为代理模型Xadmin不会自动识别权限,所以需要手动添加
1.首先查询权限代码
# 在注册的时候重写这两个方法 def get_permission_codename(self, action, opts): """ Return the codename of the permission for the specified action. """ return ‘%s_%s‘ % (action, opts.model_name) def has_view_permission(self, obj=None): flag = super(ClaimGoodsAdmin, self).has_view_permission(obj) view_codename = self.get_permission_codename(‘view‘, self.opts) # 查看权限代码 change_codename = self.get_permission_codename(‘change‘, self.opts) # 修改权限代码 del_codename = self.get_permission_codename(‘delete‘, self.opts) # 删除权限代码 add_codename = self.get_permission_codename(‘add‘, self.opts) # 添加权限代码 print(view_codename, del_codename, add_codename, change_codename) return flag
2.在后台添加权限(代码名称我改了没效果,会跟父类模型权限名称重名,不知道怎么解决,不过权限控制是可以正常实现的)
标签:查询 mamicode 控制 name long 删除权限 lis lag back
原文地址:https://www.cnblogs.com/wtil/p/12627141.html