Odoo12 字段根据用户组设置readonly属性

遇到个需求,财务审核报销单的时候,需要填写一个审核金额,此处审核金额应该在财务审核环节才能编辑,并且只能财务人员编辑。

第一反应跟compute或default一样,直接写readonly=_set_readonly,然后写函数

    @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。

经过多次尝试,想到一个曲线救国的方法,如下:

1. 定义一个boolean字段使用compute来获取当前登录用户是否是可编辑的用户组

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属性

<field name="audit_amount" attrs="{'readonly': [('current_user_is_financial', '=', False)], 'required':[('state', '=', 'to_audited')]}"/>

这样在财务人员到了审核环节的时候audit_amount字段就能编辑了,其他任何人员都不能编辑该字段。

猜你喜欢

转载自www.cnblogs.com/lnkDel/p/10967238.html