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.




Odoo Experts

on_change event in Odoo 8

Hi guys,

In this tutorial I will learn you how to create an on_change event that automatically updates a field when another field changes. In this sample I will create two new fields under sales > products in the tab ‘Procurements’. When a value changes on one of the two fields a third field will be updated with the total of the other two fields.

1. Creating new fields

The first step is to create new fields in the Python file. For example:

This adds two fields (CostPrice and ShippingCost) to the model product.template, which I first inherited. Both these fields are for numbers. After adding your new fields you need to add them to the view.

2. Add fields to view

In case you’re coding in the current module you can simply add the fields like this:

In case you have a new view you can simply add them but if you want to add them to an existing view you will need to inherit the record from the other module, just like in my sample. For example:

3. On_change function

Noticing anything in this code? I’ve already added the on_change event in the view like this:

This will search for the function on_change_price in your module and sends two values to the function, CostPrice and ShippingCost. This are the two fields that will contain the values and in these variables are now the values from the two fields. We’re almost there now, the only thing you need to do now is create the function on_change_price in your Python file. Return to your Python file and add the following code:

In the variables CostPrice and ShippingCost are the values that the user has entered. The variable total simply contains the sum of CostPrice and ShippingCost and this should now be filled in to our third field, which is named standard_price. After the value is calculated and added to the new field it should be returned so that the user can see it.
The result:
Products view - on_change fields

As you can see in this image the third field will be automatically calculated with the values from the field CostPrice and the field ShippingCost. The only thing you need to do for the on_change event to go off is to enter, tab or click anywhere else.

Want to see this module in action? You can download / view it on my Github.
If you want to you can also work with @api.onchange, which is specifically for Odoo 8.




Odoo Experts

Set default value Many2one in Odoo 8

Hi guys,

In this tutorial I will explain you how to set a default value in a Many2one relation. I will add a new field to the quotations view which is a Many2one to all the currencies to demonstrate this.
Tip: You can view my sample module here.

1. Creating the field

The first step is to create a new Many2one field that is linked to another model. Simply add a field to your .py file that creates the model. For example:

2. Adding the field to view

Then add the field to your view (.xml file):

If you now go to the view where you’ve added this field you will see a dropdown (Many2one) but there is no default value set.
Default Many2one behaviour

3. Set default value on Many2one

The next step is to go back to your .py file and add default values. Do this by adding your field between defaults {}.

As you can see this links to _get_default_currency which is another function in the same .py file. Create a function above this code which sets the value.

So what does this do? When the user wants to create a new quotation and the record opens the defaults {} will be called. This links to the field currency_id_invoices which links to the function that sets the currency in the Many2one. In this function we will go to the res.company table and search for the record where the currency_id is equal to EUR, the currency I want to set by default. When it is found it will be set in the Many2one, otherwise nothing will be set.
The result:
Default value

As you can see the currency is now filled in when you create a new record and the user can still change it whenever he wants to. Want to see this module in action? You can download / view it on my Github.
You can find more information about Many2one relations here.




Odoo Experts