【问题标题】:Odoo add records to one2many field using onchange event twiceOdoo 使用 onchange 事件两次将记录添加到 one2many 字段
【发布时间】:2019-01-04 03:01:52
【问题描述】:

作为下面的代码,当另一个字段“onte”发生更改时,我可以将记录添加到 one2many 字段“f_12m”。 但问题是,当我再次更改 'note' 值时,它会删除 'f_12m' 字段的所有记录,然后添加一条新记录。 如何在不保存整个模型的情况下保留旧记录并添加新记录?

f_12m = fields.One2many( 'x_app.other_model', 'oid', string= 'FieldName' )

@api.onchange( 'note' )
def _onchange_note( self ) : 
    dic_value = {}
    list_f_12m = []

    list_f_12m.append( ( 0 , 0 , {'note':self.note} ) ) 
    dic_value.update( f_12m = list_f_12m ) 
    return {'value':  dic_value }

【问题讨论】:

    标签: odoo one2many


    【解决方案1】:

    请尝试以下代码

    f_12m = fields.One2many( 'x_app.other_model', 'oid', string= 'FieldName' )
    
    @api.onchange( 'note' )
    def _onchange_note( self ) : 
       dic_value = {}
       list_f_12m = self.f_12m.ids
       f_12m_new = self.env['x_app.other_model'].create({'note':self.note, 'oid':self.id})
       list_f.12m.append(f_12m_new.id)
       self.f_12m = [(6,0,list_f_12m)]
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2020-08-31
      相关资源
      最近更新 更多