Odoo Help


Trying to add custome fields in One2many field.

Juan Carlos
on 12/16/14, 3:08 PM 885 views

Hi, I'm new in Odoo, I will specify my question.

I need to create a field with "add products" like a module "Sales>sales order" I read the code from the Sales Module and is to large for me.

I only want "add products" from product.product to my One2many, I also read need a OnChange. If someone have a example of this, it would be easier.

models.py field

producto_box = fields.One2many('sale.order.line', 'order_id', 'Order Lines')


 <page string="Compras">
   <field name="producto_box"/>

<!-- Need I a context for producto_box?  I try use a context but nothing changes...-->


I hope sufficient, I editing in Odoo 8, sorry my English is poor and I try to search for many places in the internet and there is nothing specific for this case.

I hope no one is annoying for help

Julian Solorzano
On 12/16/14, 4:08 PM

One2Many fields depend on a preexisting Many2One counterpart field. For example the sales module has this structure:

Orders (sale.order) have many sale lines (sale.order.line)

This means that:

sale.order.line has a many2one field which references the order. This field is called order_id.
sale.order has a one2many field which gives you all its lines. It's called order_lines. Notice that in the field definition the "order_id" field is specified. It's meant to tell Odoo which field is the one that links the line with its order (i.e. how the field is called in the other model).

So for your field you just have to identify which is the many2one and which is the one2many and make sure they are in the correct models.

Regarding onchange methods you have to do two things.

1. In the xml you have to name the method you want called using the on_change attribute.
<field name="producto_box" on_change=onchange_producto_box(xxx)/> (notice the xxx, you don't have to write those, I'll come to this later)

2.- In the model to which the producto_box field belongs you have to add a method called "onchange_producto_box(self, cr, uid, ids, xxx, context=None)"

Now where I wrote the xxx you can write the name of the fields you want the onchange method to be passed. That is, what you write in the xxx will get passed to the method, so you also have to define the method to recieve those arguments. It can be just one or more than one.

I would give you a more concrete example but I didn't understand exactly where this field will go and what the lines will be. I think you want to create a "product kit" that is make a product which contains other products. If this is the case and you simply need to directly link products with a parent then I suggest using a many2many field, or better yet search for community modules that already do this.

Juan Carlos
On 12/16/14, 5:00 PM

It is solved. 


Sorry, need 30 karma for the link.

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

1 follower(s)


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