class YourModel(models.Model):
_name = 'your.model'
_inherit = [
'protected_fields.mixin',
]
_protected_fields = ['field_you_want_to_protect']
field_you_want_to_protect = fields.Char()
forbridden_user = fields.Many2one('res.users')
current_user_forbidden = fields.Boolean(compute="_compute_current_user_forbidden")
@api.one
@api.depends('forbridden_user')
def _compute_current_user_forbidden(self):
"""
Compute a field indicating whether the current user
shouldn't be able to edit some fields.
"""
self.current_user_forbidden = (self.forbridden_user == self.env.user)
@api.multi
def _is_permitted(self):
"""
Allow only authorised users to modify protected fields
"""
permitted = super(DetailedReport, self)._is_permitted()
return permitted or not self.current_user_forbidden
这里的基本思想是,
1) 继承视图
2)指定要限制字段的组
3)然后修改字段属性。
我在这里粘贴示例代码,它将使Employee contribution字段在datateamgroup用户登录时是只读的。在
这样,您可以在特定用户登录时修改字段属性。在
这里有两个不同的问题:
这些都是独立的问题,如果只解决第一点,你将来会得到一个非常令人不快的惊喜。在
不幸的是,Odoo没有提供每个字段的权限框架(you can read my rant about this here)。在
如果你想你可以use a module I created while working on a project, that addresses this very issue。在
下载模块并将
protected_fields
添加到模块的依赖项后,您将执行以下操作:这将负责安全地保护服务器端的字段,并另外创建一个时,该字段将被设置为
current_user_forbidden
字段。当当前用户等于^{True
。我们可以在客户端使用它使受保护字段显示为只读。在将计算字段添加到您的视图中(作为一个不可见的字段-我们只需要它的值可用),并将
^{pr2}$attrs
属性添加到要保护的字段中,其中的域将使该字段在current_user_forbidden
字段为True
时显示为只读:当然,您应该使用自己想要保护的字段,而不是
field_you_want_to_protect
。在相关问题 更多 >
编程相关推荐