遇到个需求,财务审核报销单的时候,须要填写一个审核金额,此处审核金额应该在财务审核环节才能编辑,而且只能财务人员编辑。前端
第一反应跟compute或default同样,直接写readonly=_set_readonly,而后写函数api
@api.model def _set_readonly(self): if self.user_has_groups('hs_expenses.group_hs_expenses_financial_officer'): return False else: return True
这样运行的时候会直接报错,报错内容大概是当前环境没有user_has_groups对象。后来尝试写readonly=‘_set_readonly’,发现不管何时都成了False,估计readonly只要不等于True,Odoo就直接默认为False。函数
通过屡次尝试,想到一个曲线救国的方法,以下:ui
1. 定义一个boolean字段使用compute来获取当前登陆用户是不是可编辑的用户组spa
current_user_is_financial = fields.Boolean(compute="_compute_current_user_is_financial")
def _compute_current_user_is_financial(self): self.current_user_is_financial = self.user_has_groups('hs_expenses.group_hs_expenses_financial_officer')
2. 在前端经过attrs设置readonly属性code
<field name="audit_amount" attrs="{'readonly': [('current_user_is_financial', '=', False)], 'required':[('state', '=', 'to_audited')]}"/>
这样在财务人员到了审核环节的时候audit_amount字段就能编辑了,其余任何人员都不能编辑该字段。对象