Odoo Help


Create new Many2One records in @api.onchange method (2nd try)

on 12/11/14, 8:38 PM 6,877 views


I am new to Odoo and I struggle with the new Odoo API. I am trying to create new records on a Many2One field in an @api.onchange method. It is a custom module so I will simplify by providing a made up example. 

Say that I am building a new Order that has Lines. The lines are defined via a Many2One field called 'line':

class Order(models.Model):


    line = fields.One2many('mymodule.line',

    partner_id = fields.....


I want to generate new lines as soon as the user selects or changes the partner_id of the order. To do so, I have implemented an @api.onchance method that looks somewhat like this:

 def onchange_equipment_type_id(self):
        """ Updates the order lines"""
        if self.line:
        for val in self.bogus_values: #some bogus list that supposedly tells me how many lines I need to create
            self.line.create({'name':val.name, 'order_id':self.id, ....})

The above approach does not work. First of all, as far as I understand CREATE will actually create records to the DB. I do not want that. I simply want to add some order lines as if I clicked few times on 'Add an item' in a table. The actual creation of the lines shall happen when the user clicks the Save button on the order. So what method shall I use? How can I 'add' records to the line field without writing to the DB at this point? 

The second problem that I face is that self.id is NewId. Now as per my understanding this is normal for New records. But I actually get self.id as NewID even for entries that have been saved and I open them in edit mode. Anyways, for the case when the order has not been saved yet, I still need to add lines. So my major issue is how to add lines to my Many2One field as part of my onchange method without triggering a DB write. 


Let me reiterate again - I want to create a Many2One not as part of the 'parent' object create method. I want to do this as an onchange event.  I want to generate the lines as soon as I change the partner. Imagine that for each partner there is a predefined set of lines, for example based on favorite products. So as soon as the one changes the partner id, a new list of favorite products will be retrieved (for the selected partner), the existing order lines must be deleted and new lines (for the new product set) shall be generated. All this shall happen prior to the user hitting the Save button on the order object. Actually the user can change partners several times priori to saving the order. 

I hope that my question makes some sense. 

Thank you in advance for your help.

On 1/21/15, 10:37 PM

Hi, I solved this problem some time in the following manner:

def _onchange_equipment(self):
    lines =[]
    for val in self.bogus_values:
        line_item = {
                      'attr1': val.name,
                      'attr2': val.a1,
        lines += [line_item]
    self.update({'line': lines})

In my very humble oppinion this works better than the proposal of Stefano (at least for my scenario). After all, the user can change the partner many time prior to saving the order. The create will probably insert new records in the DB. We do not want this to happen each time the user changes the partner. Stefano, please correct me if I am wrong and thank you for your response.

Jairo Llopis
On 3/5/15, 7:23 AM

Do not use create(). Use new() instead. It's also more object-oriented than update().

In your case, replace self.line.create({'name':val.name, 'order_id':self.id, ....}) with self.line |= self.line.new({'name':val.name ....})

It should be documented IMHO. I opened bug https://github.com/odoo/odoo/issues/5611

Jairo Llopis
on 3/5/15, 7:30 AM

Did it for me! Thanks.

on 4/16/15, 8:09 AM

new doesn't work for me, only create works... im on odoo 8

sharon shen
on 3/2/17, 3:26 PM



| 5 3 8
Delhi, India

Nothing to say .... :)

On 1/20/15, 1:43 PM

Try this, it will work

 def onchange_equipment_type_id(self):
        """ Updates the order lines""
        for val in self.bogus_values: #some bogus list that supposedly tells me how many lines I need to create
            self.line  = [((0, 0, {'name':val.name, 'order_id':self.id, ....}))]


Stefano Del Gobbo
On 1/20/15, 6:20 AM

I need something similar.

I have to fill BoM list on selecting product.

 def onchange_product_id(self):
        """ Updates the BOM lines"""
        if self.line:
        for val in self.bom_template: #value on a BOM template that cointain products
            self.line.create({'name':val.name, 'order_id':self.id, ....})


Any help?


About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)


Asked: 12/11/14, 8:38 PM
Seen: 6877 times
Last updated: 6/8/15, 10:22 PM