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.
Hi very helpful tutorial….
Thank you Chhatrapal!
Hi Carlos,
No problem! I’m happy to hear this helped you 🙂 Thanks for the feedback!
nice, it is helpful for me.
Hi
I have some difficult to use xpath position=”attributes” for set domain for some field.
Can you help me ?
Hi, very helpful read on xpath expressions.
Is it possible to use xpath expressions on smart buttons e.g. on the customer form? I would like to add a balance field as part of the smart buttons on the partner base.view_partner_form. Here is a sample of my code
Balance
res.partner
Hi Denis,
Thanks a lot! Yep, you can do xpaths on about anything in Odoo, including Smart Buttons. Just do an xpath on the button or the header, whatever works best for you!
Any idea how to add meta tag with xpath in first occurence ?
<meta ………………
thank u
You have GREAT tutorials Yenthe! Thanks!
Thanks for the feedback Ray! Happy you like them.
A very good article about the usage of XPath in odoo.
But there is one thing that might lead to misunderstandings.
XPath is not just a part of odoo. It is part of XML, so it is of course documented. Not by odoo, but by the World Wide Web Consortion (W3C).
A good starting point to get deeper into the power of XPath is the w3school:
http://www.w3schools.com/xsl/xpath_intro.asp
Hi Martin,
Thanks a lot! You’re right that it is from W3C and worldwide. I’ll add this in my post soon.
hi..This is very helpful for me
Thank you
can u give me full code for view?? or i should just add like you mention at the post??
Hi Danial,
You should just add it as my post mensions or you can look at the code on Github.
Thanks again for a great tutorial, Yenthe!
You’re welcome, thanks for the great feedback!
Very helpful for me.
anw, can u gimme your skype, im a beginner as openerp dev ;(. ihave ton of questions @@
Hi TungNT,
Thank you! Sadly I can’t give you my Skype. I literally get dozens of requests like this a month and I really can’t keep up with the major load of questions.. Sorry, I hope you can understand that.
Hi Yenthe,
Could we able add this custom field to be printed on the quotation report .? . .If so please let me know .
Tanz in advance .
Hi Ashwin,
Any field on the model of the report (or linked field) can be used on the report.