将 Many2many 字段添加到搜索条件后的预期单例错误
Expected Singleton Error After Adding Many2many Field to Search Condition
在我的 search_count 条件中添加 many2many 字段后,我收到预期的单例错误。
该结构由 3 个 class 组成,工作、地点和雇员。我想做的是获取每个地点每个职位的员工人数。
这与 xml 中的 many2many 小部件的预期一样有效:
(不包括字符串参数)
class job(models.Model):
_inherit = 'hr.job'
rel_locations = fields.Many2many(comodel_name='hr.locations')
num_location_employees = fields.Integer(compute='_set_location_employees')
def _set_location_employees(self):
for rec in self:
rec.num_location_employees = self.env['hr.employee'].search_count([('locations', '=', 3)])
它给了我一个工作列表,其中包含 ID 为 3 的位置的员工数量。
但是,将3的id改成
后
('locations', '=', rec.rel_locations.id)
我明白了
Expected singleton: hr.locations(3,4)
这是基本位置class
class locations(models.Model):
_name = 'hr.locations'
employee = fields.One2many(comodel_name='hr.employee', inverse_name='locations')
rel_jobs = fields.Many2many(comodel_name='hr.job')
name = fields.Char(...)
我对此还是很陌生,如有任何帮助,我们将不胜感激。提前致谢。
- 您应该使用 ids 而不是 id
- 在您的域中使用 in 而不是 =
像这样:
('locations', 'in', rec.rel_locations.ids)
如果有效请告诉我。
您只能使用点符号 (.) 访问单个记录,并且那里有多个记录。检查字段访问
部分的documentation
Trying to read or write a field on multiple records will raise an
error.
rec.rel_locations 是一个 ORM one2many,包含 2 条记录到 hr.locations 模型。您首先获得了每条记录的 ID,然后像这样创建您的域:
('locations', 'in', [rel_location_id1,rel_location_id2])
有关更多信息,请查看我发布的文档部分以及:
1
在我的 search_count 条件中添加 many2many 字段后,我收到预期的单例错误。
该结构由 3 个 class 组成,工作、地点和雇员。我想做的是获取每个地点每个职位的员工人数。
这与 xml 中的 many2many 小部件的预期一样有效:
(不包括字符串参数)
class job(models.Model):
_inherit = 'hr.job'
rel_locations = fields.Many2many(comodel_name='hr.locations')
num_location_employees = fields.Integer(compute='_set_location_employees')
def _set_location_employees(self):
for rec in self:
rec.num_location_employees = self.env['hr.employee'].search_count([('locations', '=', 3)])
它给了我一个工作列表,其中包含 ID 为 3 的位置的员工数量。
但是,将3的id改成
后('locations', '=', rec.rel_locations.id)
我明白了
Expected singleton: hr.locations(3,4)
这是基本位置class
class locations(models.Model):
_name = 'hr.locations'
employee = fields.One2many(comodel_name='hr.employee', inverse_name='locations')
rel_jobs = fields.Many2many(comodel_name='hr.job')
name = fields.Char(...)
我对此还是很陌生,如有任何帮助,我们将不胜感激。提前致谢。
- 您应该使用 ids 而不是 id
- 在您的域中使用 in 而不是 =
像这样:
('locations', 'in', rec.rel_locations.ids)
如果有效请告诉我。
您只能使用点符号 (.) 访问单个记录,并且那里有多个记录。检查字段访问
部分的documentationTrying to read or write a field on multiple records will raise an error.
rec.rel_locations 是一个 ORM one2many,包含 2 条记录到 hr.locations 模型。您首先获得了每条记录的 ID,然后像这样创建您的域:
('locations', 'in', [rel_location_id1,rel_location_id2])
有关更多信息,请查看我发布的文档部分以及:
1