Odoo:从模型中搜索记录并复制到其他模型

时间:2015-08-10 11:30:23

标签: python one-to-many odoo recordset many-to-one

我在Odoo中有2个类:opc_taginstellingen en opc_actuelewaardentags。

在opc_taginstellingen我有字段tagnaam和unit。

在opc_actuelewaardentags中,我还有一个字段标签和单位。

我想要做的是比较tagnaam并从opc_actuelewaardentags中检索单位。

如果opc_actuelewaardentags.tagnaam == opc_taginstellingen.tagnaam然后检索opc_taginstellingen.unit并将其复制到opc_actuelewaardentags.unit。

我试过这样做:

class opc_taginstellingen(models.Model):
    _name = 'opc_taginstellingen'

    tagnaam = fields.Char(string="Tagnaam")
    unit = fields.Char(string="unit")
    tag_instellingen = fields.Many2one('opc_actuelewaardentags')

class opc_actuelewaardentags(models.Model):
    _name = 'opc_actuelewaardentags'

    tagnaam = fields.Char(string="Tagnaam")
    tag_instelling = fields.One2many(comodel_name='opc_taginstellingen', inverse_name='tag_instellingen')

    @api.one
    def changeUnit(self):
        instellingen = self.env['opc_taginstellingen'].search([('id','=',self.tag_instelling.id)])
        ret = ""
        for instelling in instellingen:
            ret = instelling.unit
            print ret
        return ret

    unit = fields.Char(default=changeUnit, string="unit1") 

但是这段代码不起作用。 我认为它甚至没有改变单位......

我也试过了 @api.multi代替@api.one

unit = fields.Char(compute='changeUnit', string="unit1")而不是

unit = fields.Char(default=changeUnit, string="unit1")

有没有人知道为什么这段代码不起作用?

如果我的解释不清楚,请告诉我。

1 个答案:

答案 0 :(得分:1)

class opc_taginstellingen(models.Model):
    _name = 'opc.taginstellingen'

    tagnaam = fields.Char(string="Tagnaam")
    unit = fields.Char(string="unit")
    tag_instellingen = fields.Many2one('opc_actuelewaardentags')

class opc_actuelewaardentags(models.Model):
    _name = 'opc.actuelewaardentags'

    tagnaam = fields.Char(string="Tagnaam")
    tag_instelling = fields.One2many('opc.taginstellingen')

    def changeUnit(self):
        for opc_tag in self.tag_instelling:
        if opc_tag.tagnaam==self.tagnaam:
           return opc_tag.unit

    unit = fields.Char(default=changeUnit, string="unit1")