Classical Inheritance in odoo 14
Classical Inheritance In Odoo 14
When we use or inherit an old class or model, methods, properties, and views in the new class or model, it is known as Inheritance.When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.
This blog will provide insight on How to use inheritance in odoo 14
FIELD INHERITANCE IN ODOO 14
here we are inheritting one field such as Po_number to sales order form, so we have to create one class in py file and one xml in views, give proper inheritted model name, and position , then u have to add dependency in manifest.
*: Create a new class in models file directory python file. “eg: class<ClassName>(models.Model):”
*: Instead of using _name use “_inherit”
*: Give The model name which model we have to inherit
eg: if we want to inherit “sale.order” model then we have ti give like this,
_inherit=’sale.order’
*: Now Create a new field there, like this
<field name>=fields.<type>(string='<label name>’)
eg: tax=fields.Char(string=”Tax”)
in practical case:
class AccountMove(models.Model):
_inherit = ‘sale.order’
po_number = fields.Char(”)
*: Now create a new xml file in views eg: inheritance.xml
syntax be like:
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<odoo>
<record id="sale_advance_inherit" model="ir.ui.view">
<field name="name">inherit.sales</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="field name"/>
</field> </field> </record> </odoo> Explanation: Here we need to change: <record id ="is the new record id it can be given anything. <field name="name"> is the new record name it can be given anything <field name="model"> is the model which has to be inherited. ref = is the reference form that means the name of the form we are inheriting <field name="partner_id" position="after">" this is given to give the position where we have to inherit the 'field "position="after"" it is for specifying the position of the new field. "<field name="tax"/>" this is the new field added. practical case: <record id="classical_inh_id" model="ir.ui.view"> <field name="name">classical.inh</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <field name="partner_id" position="after"> <field name="po_number"/> </field> </field> </record> Note: 1:Dont forget to add new created views in manfiset( here no need to add security if its inherited) eg: 'views/sales_inherit.xml', 2: Dont forget to add dependency in manifest such as sales , example as follows
eg : 'depends': ['base','sale'],
3:Dont forget to add reference id of inheritted form in record
u can take reference id by opening form->salesform->debug mode->edit view form->external id
After upgrading module, we will get out put as below
X-PATH INHERITANCE IN ODOO 14
we can inherit field by xpath method, we have to add one field in inherit class and add record
with xpath in corresponding xml
Note: do not forget to change record id and field position
*: Create a new class in models directory python file.
“eg: class<ClassName>(models.Model):”
_inherit=’is the model which has to be inherited.”
<field name>=fields.<type>(string='<label name>’)
in practical case:
from odoo import fields, models, api
class ClassicalInheritance(models.Model):
_inherit = ‘sale.order’
po_date = fields.Date()
*: Now create the corresponding xml file:
in practical case:
<record id="classical_inherit_id_1" model="ir.ui.view"> <field name="name">classical.inh</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='validity_date']" position="after"> <field name="po_date"/> </xpath> </field> </record> output as follows
BUTTON INHERITANCE IN ODOO 14
*: Create a new class in models directory python file.
“eg: class<ClassName>(models.Model):”
_inherit=’is the model which has to be inherited.”
<field name>=fields.<type>(string='<label name>’)
in practical case:
class ClassicalInheritance(models.Model):
_inherit = ‘sale.order’
def sample_button(self):
pass
*: Now create the corresponding xml file:
in practical case:
<record id="classical_inherit_id_btn_inh" model="ir.ui.view"> <field name="name">classical.inh</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> <xpath expr="//button[@name='action_confirm']" position="after"> <button name="sample_button" string="Sample" class="btn-primary"/> </xpath> </field> </record>>
: Now the xml files we have to add in _manifest__.py : Then Add the depended module name in the place "depends" using comma(,) : you can run the module sale.order the new added field is displayed on the form. The print will be like this
To know more about us