Enable Dark Mode!
delegation-inheritance-in-odoo-16.jpg
By: Sahla Sherin

Delegation Inheritance in Odoo 16

Technical Odoo 16

In Odoo, one of the key features is modularity. Each module in Odoo is created for specific business functions; these modules can communicate with each other. This enhances the module's capabilities. While creating some custom modules, it is better to inherit the existing module. In Odoo, different types of inheritance are there, which include view inheritance and model inheritance. Odoo has three inheritances Classical, Extension, and Delegation. So here, let's discuss the model inheritance, specifically  Delegation Inheritance.

Delegation Inheritance

In delegate inheritance, the attributes used are _inherits. The basic purpose of this inheritance is to sink other models into our custom module without varying the views.

As a result, the fields in your module and also the field from the inherited module will be present in the database table. Here we get the complete view of the inherited module too.

To get the same view of the product template form view and all fields to a custom model. First, we have to create a model and add the delegation inheritance to the model. For example, here I am creating a model named product kit and adding the model product template as delegation inheritance

class ProductKit(models.Model):
   _name = 'product.kit'
   _inherits = {'product.template': 'product_template_id'}
   _description = 'Product Package'
   _inherit = ['mail.thread', 'mail.activity.mixin']
   product_template_id = fields.Many2one
   ('product.template',string='Related_product_id',auto_join=True, 
   index=True, ondelete="cascade", required=True)
   product_bundle_ok = fields.Boolean(string='Can be Packaged',store=True)
   calculated_package_price = fields.Boolean(string='Packaged Price')
   product_ids = fields.One2many('packed.bundle', 'product_id',
                             string='Product')
Here _inherits is the syntax for the delegation inheritance
_inherits = {'product.template': 'product_template_id'}

Product.template is the model which is going to be can use in delegation inheritance, and product_template_id is a Many2one field that is linked with our custom model.

For delegation inheritance, we must declare a Many2one field of the inheriting model. In this case, all fields in the model product_template are now available in the custom model product.kit.

delegation-inheritance-in-odoo-16-1-cybrosys

We will get this view when we don’t declare the XML form view. So now we need to add the XML to get the proper view and also add the custom field view.

<record id="product_kit_normal_form_view" model="ir.ui.view">
       <field name="name">product.product.form</field>
       <field name="model">product.kit</field>
       <field name="mode">primary</field>
       <field eval="7" name="priority"/>
       <field name="inherit_id" ref="product.product_template_form_view"/>
       <field name="arch" type="xml">
           <form position="attributes">
               <attribute name="string">Product Kit</attribute>
           </form>
       </field>
</record>
Here we add the XML form view
<field name="inherit_id" ref="product.product_template_form_view"/> 

I Inherited the product_template form, and I am changing the string attributes. Now let us have a look at UI.

delegation-inheritance-in-odoo-16-1-cybrosys


Here we can see that we have the same view of the product template. Now, if we want to add new custom fields or remove already existing fields, it is very easy. Let’s see how to add new custom fields to this view.

<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
   <form position="attributes">
       <attribute name="string">Product Kit</attribute>
       <attribute name="duplicate">false</attribute>
   </form>
   <xpath expr="//field[@name='purchase_ok']" position="before">
       <span class="d-inline-block">
           <field name="product_bundle_ok"/>
           <label for="product_bundle_ok"/>
       </span>
   </xpath>
</field>
Here I am defining the XPath and adding the custom field.

delegation-inheritance-in-odoo-16-1-cybrosys

The custom field Can be packaged is now added to the view. In this way, we can remove the field existing field.

This blog will help you understand delegation inheritance and its application.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message