Creating search views with filters/groupby

< / / / / / >
V 15.0
+/- 9 minutes
Written by Yenthe Van Ginneken

Quick scroll

1. Introduction

The search view is probably the most used view in Odoo. It allows you to quickly search data, filter data or groupby a condition to find the right information that you're looking for. In this tutorial we will create a search view for contacts to show you the options of a search view and how the search view works. After this tutorial you will be able to create your own search views and understand how they work.

2. Adding the dependency

Before you can start creating search views you will need to create a new module and configure the correctly. Open up your and add 'contacts' as a dependency as we'll use contacts to demonstrate the options of search views:

                    'depends': ['contacts'],                                    

If you have an own module where you'd like to create a search view for you won't need any dependency.

3. Creating the views

3.1 Creating the tree view

A search view is always linked to another view - usually the tree view - so we'll also have to create another view. Create a new XML file named 'res_partner_views.xml' in the 'views' folder of your custom module and open the file. The first thing that we need is the tree view so let us create one for the contacts:


3.2 Creating the search view

Now that we have the tree view in place we can build our search view. In a search view you have three options to add a field:

  • Adding the field in the view so that once a user starts with typing in the search bar he/she can directly search on that field.
  • Adding a filter in the view where you set a domain to filter our records you do not want to see.
  • Adding a filter with a group_by context so that Odoo groups the results on this field.
Have a look at this search view first - which adds all three options - and I'll explain it after:


So, does this make sense? Let me explain it a bit further. Every search view its content has to be within a search tag. Inside of this search tag you will see the field with the name 'name'. This is the first possibility for search views in Odoo. It will allow the user to find contacts with the name that they've typed in the search bar in this example.
The second option is a filter. The filter will allow you to filter results with a domain. This domain functionality works just like any other domain within Odoo. In the above example we'll filter our records that have no VAT number with the domain "[('vat', '!=', '')]" for example.
The third option is to group records with the group_by option. All group_by options should be within the group tag so that they show up under the 'Group by' option at the top of your tree view. Because of the context Odoo knows on which field it has to group. For example:


This tells Odoo that the user wants to group on the 'parent_id' field (company filled in on the contact) and will group all records in your tree view based on this field. If the user searches on a second group by - such as our sales person filter - it will be applied as a second level.
Tip: Just like in any part of Odoo you can combine the context and domains and can also create complex domains.

4. Creating the menuitems and action

4.1 Creating the main menu

Alright we're almost done already! The final step is to tell Odoo which views you'd like to open when we click on a menuitem. Let's create a new menuitem in the home screen of Odoo named "Search view tutorial" and let us add a menuitem named "Contacts" under this app. Start by creating a main menu item:


4.2 Creating the action

Now let us create an action which will open the contacts in our tree view with our search view at the top of it. In order to do this we will need to create an 'ir.actions.act_window' for the partner model with the tree, form and search view available. Have a look at this code:


So, did you notice something? We've set our search view by referencing it in the 'search_view_id'. This will tell Odoo that we want to use our search view if the user opens this tree view. In search views you also have the ability to set a context and because of this we can set default filters on views! By setting "{'search_default_parent_id': 1}" in the context of this action we tell Odoo that we want the filter "parent_id" to be applied by default on the search view. This will result in all contacts being grouped by the company. You can add any filter to the context by adding 'search_default' in front of it. This will tell Odoo that it needs to apply this search filter by default once the view is opened.

4.3 Creating the contacts menu

Finally, we just need a menuitem available for the user to click on. Let's create a menuitem with our previous menuitem as a parent and let's set the action we've just created on this menuitem. Your code should look like this:


That's it! You've just created your own menu which will open a tree view for your contacts with all the search options available. If you now install your module you'll get this as a result: Result of the tree/search view when opening the menu

5. Conclusion

Search views are a very powerful way to help your users finding the content that they need. Thanks to the framework options of Odoo you can search, filter and groupby in Odoo within seconds and writing the search views is really easy too! As search views are used so often in Odoo it is a good idea to get familiar with them and to learn all their options.