Friday 13 October 2017

Using Xpath in Odoo



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



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.


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.



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

To add group to the existing groups, all you just need is to get the path of the where the group belongs, that is why in the code i had something like this //page[3]/group/group[@name='technical'], that is, on the page you want to add the group, there are two groups, a inner group and an outer group, that's why you have two groups on the xpath.



Adding Field to an Existing View


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