Delegation Inheritance In Odoo 14

DELEGATION INHERITANCE

In Odoo, we use ‘_inherits’ attribute for delegation inheritance. The purpose is for extending a model with your new model without affecting it’s views. So, the database tables will contain fields in your model and also a field representing the inherited object.

This blog will provide insight on How to use Delegation  inheritance in odoo 14

DELEGATION  INHERITANCE IN ODOO 14

 

*: Create a existing class in models  directory python file and add fields

Syntax as follows:

class<ClassName>(models.Model):”

_name=”existing model name”

<field name>=fields.<type>(string='<label name>’)

*: Now you need to create one child class for extending the existing class

eg: class<ClassName>(models.Model):”

_name=”new model name”

_inherit={‘existing model name’:’many2one field name‘}

          <field name>=fields.<type>(string='<label name>’)

         field_name = fields.Many2one(‘estimate.contract’, ‘Estimate Id’, required=True, ondelete=”cascade”)

  • *: Here one many2one field  is mandatory  to connect with parent class;while inheriting the parent class we have to give the many2one field.

In practical case:

class EstimateContract(models.Model):

      _name = ‘estimate.contract’

       p_type = fields.Many2one(‘res.partner’)

       location = fields.Char()

 

class SampleModel(models.Model):

_name = ‘sample.model’

 

_inherits = {‘estimate.contract’: ‘estimate_id’}

         name = fields.Char(‘Engineer’)

        estimate_id = fields.Many2one(‘estimate.contract’, ‘Estimate Id’, required=True, ondelete=”cascade”)

  •  Here estimate_id is the Many2one field

 

*: Now create a corresponding xml file in views

Syntax as follows:

<record id=”record _id” model=”ir.ui.view”>

        <field name=”name”>mension any name</field>

        <field name=”model”>new model name</field>

        <field name=”arch” type=”xml”>

       <form>

           <sheet>

                 <group>

<field name=”field”/>

</group>

</sheet>

</form>

</field>

</record>

In practical case:

<record id=”sample_id” model=”ir.ui.view”>

         <field name=”name”>sample.model</field>

         <field name=”model”>sample.model</field>

        <field name=”arch” type=”xml”>

        <form>

          <sheet>

             <group>

               <field name=”name”/>

               <field name=”estimate_id” required=”0″/>

             <field name=”location”/>

           </group>

        </sheet>

       </form>

     </field>

</record>

  • Here you can add field in ‘estimate.contract’ model which stores same value in both views 
  •  do not forget to add xml file  in __manifest__.py then run the module

 

The Print Like this:

 

To know more about us

 

 

 

   

Leave a Reply

Your email address will not be published.