导入时如何使odoo计算的关系字段计算自身?

时间:2019-08-04 17:06:15

标签: python python-3.x orm odoo python-import

我在视图中添加了四个字段:开始,结束,忽略和范围 范围字段是从开始字段和结束字段计算而来的,有时会使用忽略来弹出记录 该方法运行良好,但是在从Excel工作表范围字段将数据导入模式时无法计算 这就是整个代码

class relate(models.Model):
_name = 'relate'
_rec_name = 'car'

@api.multi
@api.onchange('start', 'end', 'ignore')
def years_rang(self):
    for rec in self:
        if rec.start and rec.end:
            record = [int(x) for x in range(int(rec.start), int(rec.end) + 1)]
            list = []
            if rec.ignore:
                try:
                    record.remove(int(self.ignore))
                    list = []
                except ValueError:
                    return {'warning': {'title': 'Warning!', 'message': "the Ignored year doesn't in range"}}
            for item in record:
                range_id = self.env['yearrange'].create({'name': str(item)})
                list.append(range_id.id)
            rec.rang = [(4, x, None) for x in list]
        pass
start = fields.Char(string="", required=False, )
end = fields.Char(string="", required=False, )
rang = fields.One2many(comodel_name="yearrange", inverse_name="product_id",store=True, string="Years" ,)
ignore = fields.Char(string="Ignore", required=False, ) 


class yearrange(models.Model):
    _name = 'yearrange'
    _rec_name = 'name'
name = fields.Char()
product_id = fields.Many2one(comodel_name="relate")

1 个答案:

答案 0 :(得分:1)

您的字段rang未被计算,因为您从未告诉过它。只需在字段定义上添加compute参数:

rang = fields.One2many(
    comodel_name="yearrange", inverse_name="product_id",
    compute="years_rang", store=True, string="Years" ,)

您应该在计算方法上使用api.depends而不是api.onchange

@api.multi
@api.depends('start', 'end', 'ignore')
def years_rang(self):
    # ...

在客户端,您会看到api.depends的结果与api.onchange相同。