作为下面的代码,当另一个字段"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 }
请尝试以下代码
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)]
希望这能有所帮助!