如何在记录规则中使用功能字段? [英] how to use functional field in record rule?
问题描述
我试图实现所有作为人力资源经理的用户都应被允许查看记录. 我创建了一个功能字段:
I am trying to achieve that all the users that are Hr managers should be allowed to see the records. I have created a Functional Field:
def list_HRM(self, cr, uid, ids, field_name, arg, context):
attribute = {}
hr_managers = self.pool.get('hr.employee').search(cr, uid, ['&', ('department_id.name', '=', 'Human Resources'), ('manager', '=', True)], context=context)
hr_managers_uid = []
for record in hr_managers:
hr_managers_uid.append(self.pool.get('hr.employee').browse(cr, uid, record, context=context).user_id.id)
record = self.browse(cr, uid, ids)[0]
attribute[record.id] = str(uid in hr_managers_uid or uid==1)
return attribute
_columns={
'hr_managers_func' : fields.function(list_HRM, type='char', method=True, string='List of HR Managers'),
'always_true':fields.boolean()
}
_defaults={
'always_true':True
}
在.xml文件中:
<field name="always_true" invisible="1"/>
<field name="hr_managers_func" invisible="1"/>
记录规则:
['&','|',('state','=','hod_depart'),('state','=','hr_review'),('always_true','=',eval(hr_managers_func))]
由于记录规则条件格式,我使用了字段"always_true",即 [('field_name','operator',values)]. 我认为该规则将使用eval评估功能领域 但不幸的是,eval不适用于记录规则, 我收到此错误:
I used field 'always_true' because of the record rule condition format i.e. [('field_name','operator',values)]. I thought that rule will evaluate the functional field using eval but unfortunately eval is not working on the record rule , I am getting this error:
NameError: name 'eval' is not defined
我想不出什么了. 我看到很少有论坛与我的问题有些相似,他们使用相关字段来避免记录中的功能字段,但是在这里,我必须检查当前用户是否属于hr管理者. 我试图以最好的方式对此进行解释,期待得到一些答复.
I could not think of more than this. I saw few forum somewhat similar to my problem, they were using the related field to avoid the functional field in the record, but here I have to check whether the current user belong to hr managers or not . I have tried explaining this in the best possible way, Looking forward for some reply.
推荐答案
要限制功能字段,您需要定义fnct_search.功能字段本身就变成了虚拟对象.
To restrict on a function field, you need to define a fnct_search. The functional field per se becomes a dummy.
在您的模型中:
def _my_functional_field_search(self, cr, uid, obj, name, args, context=None):
list_of_ids = [...]
return [('id', 'in', list_of_ids)]
_columns = {
'my_functional_field': fields.function(
lambda **x: True,
fnct_search=_my_functional_field_search,
type='boolean',
method=True,
),
}
然后在您的安全XML文件中:
And then in your security XML file:
<record id="your_rule_id" model="ir.rule">
<field name="name">Your Rule Name</field>
<field name="model_id" ref="model_the_model" />
<field name="groups" eval="[(4, ref('group_affected_group'))]" />
<field name="domain_force">[('my_functional_field', '=', True)]</field>
</record>
这篇关于如何在记录规则中使用功能字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!