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:
1 2 3 4 5 6 7 8 9 10 11 12 |
# -*- coding: utf-8 -*- from openerp import models, fields, api class on_change_function(models.Model): #Inhertis the model product.template _inherit = 'product.template' #Creates two new fields (CostPrice and ShippingCost) in the model product.template CostPrice = fields.Float('Buy price') ShippingCost = fields.Float('Shipping Cost') FieldAfterGroup = fields.Char(string='Field After Group') FieldNewPage = fields.Char(string='Field New Page') |
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:
1 2 3 4 |
<record id="view_product_form_inherit" model="ir.ui.view"> <field name="name">product.template.common.form.inherit</field> <field name="model">product.template</field> <field name="inherit_id" ref="product.product_template_form_view"/> |
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:
1 |
<xpath expr="//element[@string='ElementName']" position="yourPosition"/> |
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
1 2 3 4 5 6 7 |
<xpath expr="//page[@string='Information']" position="after"> <page name="Sample" string="Custom page"> <group> <field name="FieldNewPage"/> </group> </page> </xpath> |
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:
3.2 xpath group
1 2 3 4 5 |
<xpath expr="//page[@string='Information']/group" position="after"> <group> <field name="FieldAfterGroup"/> </group> </xpath> |
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:
3.3 xpath fields
1 2 3 4 |
<xpath expr="//field[@name='standard_price']" position="before"> <field name="CostPrice" on_change="on_change_price(CostPrice,ShippingCost)"/> <field name="ShippingCost" on_change="on_change_price(CostPrice,ShippingCost)"/> </xpath> |
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:
Tip: there are three options for the position tag:
1 2 3 |
<xpath expr="//field[@name='standard_price']" position="before"> <xpath expr="//field[@name='standard_price']" position="inside"> <xpath expr="//field[@name='standard_price']" position="after"> |
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.