Odoo Help

Welcome!

This community 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.

1

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

By
Tzin
on 12/11/14, 8:38 PM 4,474 views

Hello,

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

    name="mymodule.order"

    line = fields.One2many('mymodule.line',
                                inverse_name="order_id",         
                                string="Lines")

    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:

@api.onchange('partner_id')
 def onchange_equipment_type_id(self):
        """ Updates the order lines"""
        if self.line:
            self.line.unlink()
        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.

1
Tzin
On 1/21/15, 10:37 PM

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

@api.onchange('partner_id') 
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.

4
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.

Andre
on 4/16/15, 8:09 AM
2
Yogesh
On 1/20/15, 1:43 PM

Try this, it will work

@api.onchange('partner_id')
 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, ....}))]

 

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

I need something similar.

I have to fill BoM list on selecting product.

@api.onchange('product_id')
 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?

Thanks

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

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