如何在odoo 13中查找所有叶子和非叶子记录,并在每个非叶子记录上更新它们



在员工配置文件中,我添加了一个与员工表本身相关的Many2one字段(称为当前员工的报告),另一个字段是一个Many2one,它具有Reportingmanager字段的外键。

我有另一个与员工表本身相关的字段Many2many,名为All Reporter。我需要把所有记者都安排到Many2many领域,包括我的下级记者。

结构如下:

A      B       C        D
|      |       |        |
/ |  / |    / |       / 
E  F G H I  J  K L M     N   O
/|  /|          |       /
P Q R S T U        V       W X

这就是我想要的,

  • 许多A需要填充:E、F、G、P、Q、R、S、T、U
  • 许多B,需要填充:H,I,J
  • 许多C需要填充:K、L、M、V
  • 许多E需要填充:P、Q、R
  • N的许多,需要填充:W,X

这是我的代码:

reportee_ids = fields.One2many('hr.employee','reporting_officer_id',string="Reportees")
employee_child_ids = fields.Many2many('hr.employee')
def compute_reportees(self,emp,list1=None):
rm_id = emp.reporting_officer_id
if list1 is None:
list1 = []
if emp.id != rm_id.id:
list1 += emp.reportee_ids.ids + rm_id.reportee_ids.ids
rm_id.user_id.write({'employee_child_ids':list1})
self.compute_reportees(rm_id,list1)
else:
return list1

我该怎么做?

我所知道的执行这种自上而下搜索的唯一方法是使用child_of搜索域运算符。

在您的示例场景中,您可以执行以下搜索以查找给定员工记录(emp)的所有子项。

self.env["hr.employee"].search([("reporting_officer_id", "child_of", emp.id)])

结果中确实包含emp.id本身,因此如果要从结果中排除("id", "!=", emp.id),则可能需要将其附加到搜索域中。

最新更新