Odoo12 readonly attribute field is set according to a user group

We met a demand, the financial audit expense account when the amount required to complete an audit, the audit amount should be here before you can edit in the financial aspects of the audit and finance staff only editor.

The first reaction with compute or default as a direct write readonly = _set_readonly, then write function

    @api.model
    def _set_readonly(self):
        if self.user_has_groups('hs_expenses.group_hs_expenses_financial_officer'):
            return False
        else:
            return True

When this operation will direct the error, error content about the current environment is not user_has_groups object. Later attempts to write readonly = '_ set_readonly', found that no matter what time have become False, estimated readonly they do not equal True, Odoo directly defaults to False.

After several attempts, I think of a method Quxianjiuguo, as follows:

1. Define a boolean field use compute to get the currently logged on user is a user group can be edited

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. In the front end of the through readonly attribute attrs

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

 

Such personnel to audit the financial aspects when audit_amount field will be able to edit, and any other person can not edit this field.

Guess you like

Origin www.cnblogs.com/lnkDel/p/10967238.html