hoc*_*man 2 python xml python-2.7 odoo
我想使记录中的某些字段对于在字段中选择的用户不可编辑forbridden_user。但并非所有领域。对于他来说,某些字段仍必须可编辑。我该如何实现?
这里有两个单独的问题:
这些是单独的问题,仅解决第一点,您将来会感到非常不愉快。
不幸的是,Odoo没有附带每个字段的权限框架(您可以在此处阅读有关此内容的内容)。
下载模块并添加protected_fields到模块的依赖项后,您将执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
这将确保在服务器端安全地保护该字段,并另外创建一个current_user_forbidden字段。当前用户等于True时,该字段将设置为。我们可以在客户端使用它来使受保护的字段显示为只读。forbridden_user
将计算字段添加到视图中(作为不可见字段-我们只需要提供其值即可),然后将attrs属性添加到要保护的字段中,并使用一个域,使该字段在该current_user_forbidden字段显示为只读到True:
<field name="current_user_forbidden" invisible="1"/>
<field name="field_you_want_to_protect" attrs="{'readonly': [('current_user_forbidden', '=', True)]}"/>
Run Code Online (Sandbox Code Playgroud)
当然,field_you_want_to_protect您应该使用自己想要保护的字段来代替您。