Odoo12 字段根據用戶組設置readonly屬性

遇到個需求,財務審覈報銷單的時候,須要填寫一個審覈金額,此處審覈金額應該在財務審覈環節才能編輯,而且只能財務人員編輯。前端

第一反應跟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字段就能編輯了,其餘任何人員都不能編輯該字段。對象

相關文章
相關標籤/搜索