This question has been flagged
2 Replies
3501 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')

archive.xml

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

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


 </page>
</notebook>

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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author Best Answer

It is solved. 

https://www.odoo.com/forum/help-1/question/integrity-error-one2many-with-many2one-solved-70797

Sorry, need 30 karma for the link.

Avatar
Discard