Enable Dark Mode!
search-view-and-filters-in-odoo-15.jpg
By: Sruthi M

How to configure Search View and Filters and Group by in Odoo 15

Technical Odoo 15

In Odoo there are different view types like Form, Kanban, Tree, Calendar, Qweb, Search, etc.  Search views are different from other views because Search views will not display the content and are used to filter the contents of other views rather than Form views. Because Form views consist of a single record so there is no need for any search.

This blog will provide insight into the Search views and filters in Odoo 15.

search-view-and-filters-in-odoo-15

The marked portion is the search view in Odoo. We can add the field names for the search and if we search for something that is available in the view then we get the results based on the corresponding field name. Then we can check how to create a Search View in Odoo 15. Like Form, Tree, and Kanban views we can add a search view on an XML file inside the views folder.

For example, here we have already created a model school.student and also defined the list and form view for this model. Now we can create a search view for this model school.student.

school_student_views.xml

<record id="school_student_search" model="ir.ui.view">
        <field name="name">school.student.search</field>
        <field name="model">school.student</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="email"/>
                <field name="age"/>
                <field name="gender"/>
            </search>
        </field>
    </record>

We define all the search views inside the search tag.  From here we gave the field names: name, email, age, and gender for search. Based on these search fields we get all the search results.  Then we can upgrade the module and will get the result as follows:

search-view-and-filters-in-odoo-15

We get the search result based on these field names.

search-view-and-filters-in-odoo-15

Here we searched the results based on the name. And we have only one record with this name.

Filters and Group By Options

We also have the options Filters and Group By in the Search view. We can filter and also can group the records by the fields of the corresponding model. If we click on these options then we can see all the filters corresponding to the model. We want to choose Add Custom Filter or Add Custom Group for all the time. Sometimes it is not easy.

search-view-and-filters-in-odoo-15


search-view-and-filters-in-odoo-15

We have an easy way of adding filters by default. We can check how we can do this.

<record id="school_student_search" model="ir.ui.view">
        <field name="name">school.student.search</field>
        <field name="model">school.student</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="email"/>
                <field name="age"/>
                <field name="gender"/>
                <field name="active"/>
                <filter string="Archived" name="active" domain="[('active', '=',False)]"/>
            </search>
        </field>
    </record>

Here we have added a filter for getting all archived records. In case the name is a required attribute, we can give any names on this name attribute. Additionally, a domain name should be given for the filter because filtering will be done based on this domain, i.e. here the filter records if the field active is false. 


search-view-and-filters-in-odoo-15

Here we have only one archived record i.e., the active field is false only for this record that's why we get this result. So we don’t need to click Add Custom Filter for filtering the records and also we can click more than one filter at a time.

In the case of Group By we can give the filters inside the group tag.

<record id="school_student_search" model="ir.ui.view">
        <field name="name">school.student.search</field>
        <field name="model">school.student</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="email"/>
                <field name="age"/>
                <field name="gender"/>
                <field name="active"/>
                <filter string="Archived" name="active" domain="[('active', '=', False)]"/>
                <group expand="0" string="Group By">
                    <filter string="Gender" name="gender" context="{'group_by': gender}"/>
                </group>
            </search>
        </field>
    </record>

From the code, we use a group by gender and we use context to pass the data. Here also name is required and grouping is done by the gender field. We can use any field names of the corresponding model for grouping.

search-view-and-filters-in-odoo-15

This is the result for the group by gender. Inside the female group, we can see all the female student records and the same as in the case of the male group. This way we can add other fields also for grouping.

So we can customize our search in this way. These are the important things for creating and customizing search views in Odoo 15.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message