Help

2

Odoo 10 : Customise many2many relation table

Hi guys,

Assume that we have 2 tables :

- products

- purchase_orders

With a many2many relation.

In the purchase order creation I need to insert X products.

But I'm stuck trying to add a quantity or a number field with a total price of each product in a specific purchase.

To achieve this, I customize the many2many relational table :

purchase_orders_id = fields.Many2many(comodel_name='tjara.products', relation='products_orders_rel', column1='product_id', column2='purchase_order_id', string='Purchase Orders')

I added some other fields in the products_orders_rel as quantity and total_price.



# -*- coding: utf-8 -*-
from odoo import models, fields, api

class Productsordersrel(models.Model):
   _name = 'tjara.
products_orders_rel'
 
   quantite = fields.Float(
       string='Quantite',
       required=True,
       default=1.0,
       digits=(16, 3)
   )
   
   purchase_order_id = fields.Many2one(
        required=True,
        index=True,
        comodel_name='jara.purchase_orders',
         
string='Purchase Order'
)
   product_id = fields.Many2one(

index=True,
required=True,
        comodel_name='tjara.product_id',
        string='Product'
    )


How can I fill these fields from the purchase_order view ?

<field name="products_ids">
<tree editable="bottom">
<field name="name"/><!-- Display the product name -->
<field name="code_product"/><!-- Display the product code -->
<field name="price_product"/><!-- Display the product price -->
<field name="products_orders_rel.quantity"/> <!-- The quantity field from products_orders_rel -->
<field name="products_orders_rel.total_price"/> <!-- total_price from products_orders_rel = price * quantity -->
</tree>
<form>
<group>
<field name="name">
</group>
</form>
</field>

 

1 Comment
Avatar
Discard
Avatar
Zied Haffoudhi
-

Edit :

product_id = fields.Many2one(

index=True,

required=True,

comodel_name='tjara.products',

string='Product'

)

2 Answers
0
Avatar
Antoine Labalette
Best Answer

Hey, @Khidma!

Could you post your solutions please? I'm trying to do something very similar and I don't understand what you found out here.

Avatar
Discard
0
Best Answer

Create another table purchase_orders_line to save quantity, subtotal and maybe discounts or more info per product.

- products

- purchase_orders

- purchase_orders_line

Each  purchase_orders, has several purchase_orders_line and each line reference products with details like you want. (dive into the logic for invoices module.)

3 Comments
Avatar
Discard
Avatar
Zied Haffoudhi
-

Thank you for your reply.

That's what I already did with 'products_orders_rel' table but I didn't figure out how to modify its fields from the tree. 'products_orders_rel.quantity' didn't work.

If you review the core Odoo code for the purchase.order and purchase.order.line models, you should be able to find exactly the piece that you are needing.

Avatar
Zied Haffoudhi
-

@Travis, Thank you for your help, very useful reference. I'm taking a deep look there.