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>
Edit :
product_id = fields.Many2one(
index=True,
required=True,
comodel_name='tjara.products',
string='Product'
)