Odoo add records to one2many field using onchange event twice

3.3k Views Asked by At

As code below , I could add record to the one2many field 'f_12m' when the other field 'onte' was changed . But the problem is that when I change the 'note' value again , it delete all the records of 'f_12m' field , then add a new record . How can i keep the old records and add new one , without saving the whold model ?

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

There are 1 best solutions below

0
On BEST ANSWER

Please try below code

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)]

Hope this helps!