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

2024-09-19 22:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在奥多有两门课:opc_taginstellingen en opc_actuelewaardentags。在

在opc_taginstellingen中,我有tagnaam和unit字段。在

在opc_actuelewaardentags中,我还有一个字段tagnaam和unit。在

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

如果opc_塔格纳姆学院==opc_taginstellingen.tagnaam公司然后检索opc_taginstellingen.单位复制到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而不是{} 以及

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

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

有人知道为什么这个代码不起作用吗?在

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


Tags: nameselffieldsstringtagunitretchar
1条回答
网友
1楼 · 发布于 2024-09-19 22:11:51
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") 

相关问题 更多 >