how to use functional field in record rule?

懵懂的女人 提交于 2020-01-03 05:58:08

问题


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
      }

In .xml file:

<field name="always_true" invisible="1"/>
<field name="hr_managers_func" invisible="1"/>

In Record Rule:

['&','|',('state','=','hod_depart'),('state','=','hr_review'),('always_true','=',eval(hr_managers_func))]

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

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.


回答1:


To restrict on a function field, you need to define a fnct_search. The functional field per se becomes a dummy.

In your model:

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,
    ),
}

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>


来源:https://stackoverflow.com/questions/17722919/how-to-use-functional-field-in-record-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!