Creating searchpanel views in Odoo
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. Since Odoo 13 there is a new option 'searchpanel' to add within a search view. In this tutorial we will create a search view for contacts to show you the options of a search view and how to add a searchpanel to the search view. After this tutorial you will be able to create your own searchpanels and understand how they work.
Before you can start creating search views with a searchpanel you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add 'contacts' as a dependency as we'll use contacts to demonstrate the options of search views:
If you have an own module where you'd like to create a search view for you won't need any dependency.
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:
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:
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.
Finally, there is the 'searchpanel' block. By adding the searchpanel within the search view Odoo knows that you want to add the search panel at the left of your views so users can easily filter. Within the 'searchpanel' block you can define any of the fields you have on a model (except for many2many fields!) and apply an icon to it. The icon will be rendered to the left of the title. For example:
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 "Searchpanel tutorial" and let us add a menuitem named "Contacts" under this app. Start by creating a main menu item:
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.
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:
As you can see Odoo dynamically creates the searchpanel at the left. It will show all the possible values for the field in the database and allows you to quickly filter by selecting a value.
Search views combined with searchpanels are a very powerful way to help your users finding and filtering through 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! By simply adding a 'searchpanel' block the user can filter easily on the fields and find data even faster. 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. Searchpanels can be a great addition here!