I have three table :
- product.
- package.
- product_package.
The product_package table have 2 fields which are related to the product and package as it mentionned bellow :
product_id = fields.Many2one('tjara.product', ondelete='cascade', string="Produit", index=True)
package_id = fields.Many2one('tjara.package', ondelete='cascade', string="Emballage", index=True)
_sql_constraints = [
('ligne_unique', 'unique (product_id, package_id)', '%(package_id)d Package is already exists for this product...!')
]
I added the _sql_contraints to avoid the duplicate fields. Because it can be only an unique relation between the product and a package.
And this is what I have in the product model :
product_package_ids = fields.One2many('tjara.product_package', 'product_id', string='Package')
And this is how to add a package from the product view :
<page string="Package">
<label for="product_package_ids" string="Emballage"/>
<field name="product_package_ids">
<tree>
<field name="package_id"/>
<field name="price"/>
</tree>
<form>
<field name="package_id"/>
<field name="price"/>
</form>
</field>
</page>
This works fine for me. There is only one thing I want to enhance. When the user select an existing package for the product there is no error triggered. The error popped up only when you click "save the product".
I want either to display only the unexisting packages in the select field package_id. Or to pop up an error when the user try to add an existing package for the product.