Adding filters to existing search views

Hi guys,

In this tutorial I will teach you how to add filters to existing quick search options. In this tutorial I will add two filters to the quick search view so that you can filter  out quotations based on the total of the quotation. The one filter will only show quotations that have a higher total than 1.000, in the other filter I will only show quotations that have a total under 1.000. An example of the result after this tutorial:
Quick filters example

1. Inheriting the search view

The first thing to do is to find the search view where you want to add your own filters in. The easiest way to do this is to activate developer mode in Odoo, opening up the developer icon and by clicking on ‘Edit SearchView’:
Edit searchview

After you’ve clicked on this menuitem a dialog will pop-up where you can find all the details from the view. Now search for the field ‘External ID’ and copy the value from this field:
View id
This value is the ID to this search view, which you will need to inherit in order to add your custom filter in it later on.
In my example the ID of this view is ‘sale.sale_order_view_search_inherit_quotation’.

Now open up (or create) an XML file and inherit the existing search view like this:

What exactly does this code do? The record id will be the unique id for this view in the database and the model ‘ir.ui.view’ tells Odoo that it is a view. In the field name=”name” tag you should add an unique name for the record. The model refers to the model of the view you’re working on (in this tutorial sale.order) and the inherit_id field refers to the view from which we just copied the ‘External ID’.
Next we’ll add an arch field in which we will later on add the filter in when we want to quick filter on it.

Adding your own filters to the quick search

Alright, you’ve now inherited the view. The only thing left to do now is to add the filters on which you want to quick filter. In order to add the filters in the search you should xpath in to the search element of the view to add your own filters in. So you’ll do an xpath on the view with a position=”inside”. Inside this xpath expression you can then add the filter(s) you’d like. Your code should look like this:

That is all! When you install this module you’ll see the filters that you’ve just added show up in the quick search options. In my example it’ll look like this:
Quick filter result
After using the filter that only shows the quotations that have a total under 1.000:
Matching quotations with a total under 1.000
After using the filter that only shows the quotations that have a total over 1.000:
Matching quotations with a total over 1.000

3. Conclusion

Inheriting and expanding search views to add filters is really easy to do in Odoo and it is very powerful. You can simply inherit the view, xpath in to the search part and add all the filters that you’d like to be available by default.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!




PayPal

Odoo Experts