Odoo使用onchange事件两次将记录添加到one2many字段中

时间:2018-07-27 03:20:49

标签: odoo one2many

如下所示,当另一个字段“ onte”更改时,我可以将记录添加到一个字段“ f_12m”中。   但是问题是当我再次更改'note'值时,它将删除'f_12m'字段的所有记录,然后添加一个新记录。   我该如何保存旧记录并添加新记录,而又不保存whold模型?

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 }

1 个答案:

答案 0 :(得分: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)]

希望这会有所帮助!