Odoo Help


Create new Many2One records in @api.onchange method

on 12/8/14, 10:23 PM 2,732 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((0, 0, {'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. 

I hope that my question makes some sense. 

Thank you in advance for your help.

Hi Tzin, can you please explain your need to do this?? to know more about the question

Baiju KS
on 12/8/14, 11:14 PM

Thanks Baiiju, see my last comment for more details.

on 12/8/14, 11:56 PM

Hi Ivan, Thank you very much for your response. Unfortunately, this is not what I am looking for. Initially I had it as self.line.create({'name':val.name, 'order_id':self.id, ....}). It did not work either - the self.id was NewID regardless if the Order was newly created or if it was an existing order that I was editing. I think there is something funny happening in @api.onchange. Now, the second proposal will not for me either. 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. This should also answer the clarification request from Baiiju. I believe that the best place is an onchange method but I am struggling in creating new Many2One-s inside the onchange handler. Reading Many2One is not a problem. And last but not least, I do not think I shall use create as it writes to the DB.

on 12/8/14, 11:56 PM



| 5 3 6
Jakarta, Indonesia
On 12/8/14, 11:41 PM

You are mixing the syntax for 2many with create.  If you want to create the line record, don't use (0, 0, ...) the dictionary itself would do. So self.line.create({'name':val.name, 'order_id':self.id, ....}).  However this will presume that the order has been created.

If you want to create it together with the mymodule.order, then you use that syntax:
self.mymodule_order.create({.... 'line': [(0, 0, {'name':val.name, ....})]}) and you don't have to specify the 'order_id' because it will be added automatically.  Also you need to put the (0, 0, ...) tuple in a list.

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

2 follower(s)


Asked: 12/8/14, 10:23 PM
Seen: 2732 times
Last updated: 3/16/15, 8:10 AM