【问题标题】:Odoo: Search records from model and copy to other modelOdoo:从模型中搜索记录并复制到其他模型
【发布时间】:2015-08-10 11:30:22
【问题描述】:

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

在 opc_taginstellingen 我有字段 tagnaam 和 unit。

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

我想做的是比较 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")

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

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

【问题讨论】:

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


    【解决方案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") 
    

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 2015-03-28
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 2020-02-03
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多