Many2many 字段依赖于另一个 Many2many 字段

Many2many field depends on another Many2many field

在我的模块中,我需要当 select 个部门时,只有 select 个部门的员工出现

        

employee_id0 = fields.Many2many('hr.employee', string='Employee', index=True)
department_id = fields.Many2many('hr.department', string="Department", store=True, required=True)
 

<field name="department_id"  attrs="{'readonly':[('affec_type','=','category')],'invisible':[('affec_type','=','category')]}" widget="many2many_checkboxes"/>
<field name="employee_id0"  attrs="{'invisible':[('affec_type','=','category')]}"  widget="many2many_checkboxes"  domain="[('department_id', 'in',department_id)]"/>
                                

编写department_idon_change方法。并获取与您选择的部门相关的所有员工。而 return 到 employee_id.

这只是一个示例代码。

@api.depends('department_id')
@api.onchange("department_id")
def _onchange_department_id(self):
    vals = {}
    data = self.env['hr.employee'].search([('departmentt_id','in',self.department_id.ids)])
    ids = list(data.ids)
    vals['domain'] = {
        "employee_id": [("id", "in", ids)],
    }

    return vals

希望对您有所帮助。