Hi Odoo Developers,
In this tutorial i will teach you how to use Xpath expression. Xpath expressions give you the ability to add new pages, groups, fields, to an already existing view.
Adding pages, groups and fields to an existing view is not really complicated, but involves more codes unlike adding more key values to a selection field.
The odoo standard way of adding xpath is simply through this format;
<xpath expr="//element[@name='ElementName']" position="yourPosition"/>
To do this,
Step 1: Inherit Existing Model and Create New Fields
To add fields to an existing model, you need to inherit an existing model and add more fields to the model. For the sake of this tutorial, I will be using the sale.order model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import fields, models, api | |
class SalesOrder(models.Model): | |
# inherits the sale order model | |
_inherit = 'sale.order' | |
# Newly created fields | |
page_field = fields.Char(string="Page Field") | |
group_field = fields.Char(string="Group Field") | |
new_field = fields.Char(string="New Field") |
Step 2: Inherit Existing View
To inherit an existing view means, you have to call up the existing model with the id as the ref just like in the code below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<odoo> | |
<data> | |
<record id="sale_order_form_inherit" model="ir.ui.view"> | |
<field name="name">sale.order.form.inherit</field> | |
<field name="model">sale.order</field> | |
<field name="inherit_id" ref="sale.view_order_form"/> | |
<field name="arch" type="xml"> | |
<!-- Add your code here --> | |
</field> | |
</record> | |
</data> | |
</odoo> |
Adding Page to an Existing View
To add a new page to the sale.order model, you need to get the name or id of the existing page. For example, on the Sale Order, the Pages available are Order Lines and Other Information. We will add a new page between the Order Lines and the Other Information, and call the new page New Page and I will add a new field, so the page won't be empty.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xpath expr="//page[1]" position="after"> | |
<page string="New page"> | |
<group> | |
<field name="page_field"/> | |
</group> | |
</page> | |
</xpath> |
In the format above, you will notice there is //element[@name='ElementName'] but in the code no @name='ElementName' was found, that's because the page does not have a name and you can't use the string on a page to add a new page, it will throw errors. So you need to observe well.
Adding Group to an Existing View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xpath expr="//page[3]/group/group[@name='technical']" position="after"> | |
<group string="New Group"> | |
<field name="group_field"/> | |
</group> | |
</xpath> |
Adding Field to an Existing View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xpath expr="//field[@name='partner_id']" position="after"> | |
<field name="new_field"/> | |
</xpath> |
The code shows how to add a new field called New Field after the existing field called Customer. That was simple and straight forward, just the element and the name of the field //field[@name='partner_id']
Conclusion
To wrap this up, i will like to put all the xml, changes together, for simplicity;
I hope you enjoy the tutorial. Kindly drop comment below, if you have anything to share and do kindly share.
No comments:
Post a Comment