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:
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’:
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<odoo> <data> <!--Inherit quotations search view--> <record id="view_sale_order_inherit_search" model="ir.ui.view"> <field name="name">sale.order.search.expand.filter</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.sale_order_view_search_inherit_quotation"/> <field name="arch" type="xml"> </field> </record> </data> </odoo> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!--Inherit quotations search view--> <record id="view_sale_order_inherit_search" model="ir.ui.view"> <field name="name">sale.order.search.expand.filter</field> <field name="model">sale.order</field> <field name="inherit_id" ref="sale.sale_order_view_search_inherit_quotation"/> <field name="arch" type="xml"> <xpath expr="//search" position="inside"> <!-- This field will show up on the default filters < is the XML encoding for < and > is the XML encoding for > --> <filter string="Total < 1000" name="total_under_1000" domain="[('amount_total', '<', 1000)]"/> <filter string="Total >= 1000" name="total_above_1000" domain="[('amount_total', '>=', 1000)]"/> </xpath> </field> </record> |
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:
After using the filter that only shows the quotations that have a total under 1.000:
After using the filter that only shows the quotations that have 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!