Xpath expressions in Odoo 8

Hi guys,

In this tutorial I will learn you how to use xpath expressions. Xpath expressions give you the ability to add new pages, groups, fields, … to an already existing view. When you inherit a currently existing view in a module and want to extend it you will absolutely need xpath expressions. In this sample I will add new pages, groups and fields to the view under sales > products.

Tip:view my sample xpath module.

1. Create new fields

The first step is to create new fields inside your model. For example:

2. Inherit existing view

When you’ve created your fields you need to show them. In this example I will inherit the view from the product module which is showed under sales > products. You can reference another view with the ref=”” tag. The first step is to find the name of the view you want and then adding the module name in front of it. For example:

This now links to the product module to the view product_template_form_view (which you can find in product_view.xml under the product module). After we’ve inherited the view we can add the xpath expressions.

3. Xpath expressions

xpath expressions are basically paths to the page / group / field inside the view that you’ve inherited and are always built in the same way. Something along these lines:

Since the syntax is always a bit different I will simply give an example of each. The idea is to give the name from the page / group / field / .. so that Odoo can find the correct location to insert your new fields.

3.1 xpath page

This code will create a new page after the currently existing page named ‘Information’. The new page will be named ‘Custom page’ and will show the field FieldNewPage.
Which will result in the following page:
xpath page

3.2 xpath group

This code will create a new group after the group, inside the page with as title ‘Information’ and will show the field ‘FieldAfterGroup’ in it.
Which will result in the following page:
xpath group

3.3 xpath fields

This code will show the fields ‘CostPrice’ and ‘ShippingCost’ after the field ‘standard_price’. This is by far the easiest xpath expression and in usual cases this will work just fine.
This code will result in the following page:
xpath fields

Tip: there are three options for the position tag:

These are the most used xpath expressions but there are many more, as you can see in the source code of Odoo.
xpath is very powerfull and has a lot of options but sadly there is barely any documentation about it. I’ve created a sample module which you can view on my Github account.
Tutorial sponsored by Oocademy