How To Inherit View and Model in Odoo
Posted By : Aakash Vishwakarma | 30-Nov-2022
How To inherit Model, view, in the existing module in Odoo 14.
Suppose I want to add a field in the existing table and view we need to create XML file or .py file, .xml file to inherit view and .py file to inherit the table.
Inherit the table and add a field.
Let’s Create a file with a .py extension. And now write the below code.
Syntax:
Class Class_name(models.Model):
_inherit = ‘table name’
This is done by creating a new py file in which you create a model. In the py file, you add the below code to add a field in the sale order table
class SaleOrder(models.Model):
_inherit = "sale.order"
note = fields.Char(string=’Note’)
Inherit a View and add a field.
Let’s Create a file with a .xml extension. And now write the below code.
Syntax:
<record id="uniq_id" model="ir.ui.view">
<field name="model">table.name</field>
<field name="inherit_id" ref="module_name.inherit_view_id"/>
<field name="arch" type="xml">
<xpath expr="path of location which we want to inherit" position="replace" >
<field name="field_name" />
</xpath>
</field>
</record>
This is done by creating a new module in which you add an XML file. In the XML file, you add a record with an inherit_id that links to the original view. An example to inherit the form view from a Sale Order:
here in id, we give an unique id for this record, in the model field we mention the table name in the inherit_id field we mansion the view in which we want to inherit and finally we give a path in XPath tag where we want to add over the new field.
This record will save in ir.UI.view table. This ir.ui.view table is used to store all the views in odoo.
Suppose I want to add a notes field in the sale order form view.
<record id="sale_order_tree_inherit_view" model="ir.ui.view">
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_quotation_tree_with_onboarding"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='user_id']" position="replace" >
<field name="sale_executive_id" />
</xpath>
</field>
</record>
Extra Point: if you want to add create a module in odoo run the below code
python3 odoo-bin scaffold module_name addons/.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aakash Vishwakarma
Aakash Vishwakarma is a highly skilled backend developer with years of industry experience. He has a comprehensive understanding of the latest technologies and practical experience working with tools and frameworks like Python Django, Django Rest Framework, Odoo14, Odoo15, HTML, CSS, JavaScript, and Jquery, as well as databases. He has contributed to the successful delivery of various client projects, including Pando Mall, Hatrik Marketplace, My Mandi, File Comparison, and TRO Projects. Aakash has a creative mindset and strong analytical skills that enable him to think critically and explore new technologies with ease, making him an asset to any project