How to Define Search Views in Odoo and Add Filter and Group-By Options

Abid Patel
31-Aug-2024 Updated : 31-Aug-2024

Master the art of defining search views in Odoo. Learn how to add filters and group-by options to enhance data management and streamline your Odoo interface.

How to Define Search Views in Odoo and Add Filter and Group-By Options

Odoo, an open-source ERP platform, is known for its flexibility and modularity, which allows developers to create customized business solutions. One of the essential features in Odoo is the search view, which enables users to filter and group records efficiently. In this blog, we will explore how to define a search view in Odoo and add custom filter and group-by options. We will avoid discussing tree views, forms, or setting up your Odoo development environment, focusing solely on search views.

Step-by-Step Guide How to Define Search Views in Odoo and Add Filter and Group-By Options:

1. Understanding the Basics of Search Views 
2. Defining a Search View 
3. Adding Custom Filters 
4. Adding Group-By Options 
5. Enhancing Search Views with Advanced Features 
6. Testing and Debugging Your Search View 
7. Conclusion

Introduction to Search Views in Odoo

In Odoo, search views are used to define the search interface for models. They allow users to filter, search, and group data in list views and other aggregated views. The search view is an XML structure that defines the available search fields, filters, and grouping options.

Key Components of a Search View

Search Fields: These are fields that users can search on. They are defined using the tag.

Filters: Filters allow users to apply specific conditions to filter data.

Group-By: Group-By options let users categorize data into different groups.

1. Understanding the Basics of Search Views

Before diving into customization, it's important to understand the basic structure of a search view in Odoo. A search view is defined in an XML file associated with a model. Here is an example of a simple search view definition:

xml

<record id="view_search_my_model" model="ir.ui.view">
    <field name="name">my.model.search</field>
    <field name="model">my.model</field>
    <field name="arch" type="xml">
        <search>
            <field name="name"/>
            <filter string="Active" name="active" domain="[('active', '=', True)]"/>
            <group expand="1" string="Group By">
                <filter string="Category" name="group_by_category" context="{'group_by':'category_id'}"/>
            </group>
        </search>
    </field>
</record>

In this example:

'' allows users to search by the name field.

'' adds a filter to show only active records.

'' adds a group-by option for categorizing records by 'category_id'.

2. Defining a Search View

To define a search view, follow these steps:

- 'Create an XML file' Define a new XML file in your module's 'views' directory. This file will contain the search view definition.

- 'Define the search view structure' Use the '

' tag to start defining your search view. Inside this tag, add fields, filters, and group-by options as needed.

Here's an example of defining a search view for a model named 'my.model':

xml

<record id="view_search_my_model" model="ir.ui.view">
    <field name="name">my.model.search</field>
    <field name="model">my.model</field>
    <field name="arch" type="xml">
        <search>
            <!-- Search fields -->
            <field name="name"/>
            <field name="description"/>
            
            <!-- Filters -->
            <filter string="Active" name="active" domain="[('active', '=', True)]"/>
            <filter string="Inactive" name="inactive" domain="[('active', '=', False)]"/>
            
            <!-- Group-By -->
            <group expand="1" string="Group By">
                <filter string="Category" name="group_by_category" context="{'group_by':'category_id'}"/>
                <filter string="Status" name="group_by_status" context="{'group_by':'status'}"/>
            </group>
        </search>
    </field>
</record>

Explanation:

Search Fields: The '' tags define the fields that can be searched. In this case, users can search by 'name' and 'description'.

Filters: The '' tags define specific filters that users can apply. The 'domain' attribute specifies the condition for the filter. For example, the "Active" filter shows only records where 'active' is 'True'.

Group-By: The '' tag allows users to group records by specific fields. In this example, users can group records by 'category_id' and 'status'.

3. Adding Custom Filters

Static filters are defined using the '' tag with a 'domain' attribute. Here's an example of adding a static filter to show only records created in the last 30 days:

xml

<filter string="Last 30 Days" name="last_30_days" domain="[('create_date', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>

In this example:

The filter is named "Last 30 Days".

The 'domain' attribute filters records where 'create_date' is within the last 30 days.

Dynamic Filters

Dynamic filters can change based on user input or context. For example, you might want to filter records based on a date range selected by the user. Here’s how you can define a dynamic filter:

xml

<filter string="Custom Date Range" name="custom_date_range" domain="[]" context="{'search_default_custom_date_range': True}">
    <separator string="Date Filters"/>
    <field name="start_date" filter_domain="[('create_date', '>=', self)]"/>
    <field name="end_date" filter_domain="[('create_date', '<=', self)]"/>
</filter>
Explanation:

- Static Filters: Use predefined conditions to filter records. They are easy to implement and are useful for common scenarios like filtering by active/inactive status.

- Dynamic Filters: Allow more flexibility by letting users input custom values to filter records.

4. Adding Group-By Options

Group-by options in Odoo allow users to categorize records by specific fields. This feature is particularly useful when analyzing data in list views.

Adding Basic Group-By Options

To add a group-by option, use the '' tag with the 'context' attribute. Here’s an example of adding a group-by option for a 'category_id' field:

xml

<filter string="Category" name="group_by_category" context="{'group_by':'category_id'}"/>
Adding Multiple Group-By Options

You can add multiple group-by options by defining multiple '' tags inside the '' tag. Here’s how:

xml

<group expand="1" string="Group By">
    <filter string="Category" name="group_by_category" context="{'group_by':'category_id'}"/>
    <filter string="Status" name="group_by_status" context="{'group_by':'status'}"/>
</group>
Explanation:

- Basic Group-By: Adds a single group-by option for a specific field.

- Multiple Group-By: Allows users to group records by different fields, providing a more detailed analysis.

5. Enhancing Search Views with Advanced Features

Odoo allows developers to enhance search views with advanced features like auto-apply filters and default group-by options.

Auto-Apply Filters

You can configure filters to be applied automatically when the search view loads. This is done using the 'context' attribute. Here’s an example:

xml

<filter string="Active" name="active" domain="[('active', '=', True)]" context="{'search_default_active': 1}"/>

In this example:

- The "Active" filter is applied by default when the search view loads.

Default Group-By

You can also set a default group-by option using the 'context' attribute. Here’s how:

xml

<filter string="Category" name="group_by_category" context="{'group_by':'category_id', 'search_default_group_by_category': 1}"/>

In this example:

- The records are grouped by 'category_id' by default when the search view loads.

Explanation:

- Auto-Apply Filters: Useful for preloading search views with common filters, saving users time.

- Default Group-By: Automatically groups records by a specific field when the search view is loaded, improving the user experience.

6. Testing and Debugging Your Search View

After defining your search view, it’s crucial to test and debug it to ensure everything works as expected. Here’s how:

Testing the Search View

- Load the Module: Install or upgrade your module in Odoo to load the new search view.

- Navigate to the Model’s List View: Open the list view for the model associated with your search view.

- Test Filters and Group-By Options: Apply the filters and group-by options you’ve defined to ensure they work correctly.

Debugging Common Issues

- Incorrect Domains: If your filters are not working, check the 'domain' attributes to ensure they are correctly defined.

- Missing Fields: Ensure that the fields you reference in the search view exist in the model.

- Context Issues: If default filters or group-by options are not applied, verify the 'context' attributes.

Explanation:

- Testing: Ensures that the search view works as expected and meets user requirements.

- Debugging: Helps identify

and resolve issues with the search view.

6. Conclusion

Defining search views and adding filters and group-by options in Odoo is a powerful way to enhance data management and user experience. By following the steps outlined in this blog, you can create customized search views that allow users to filter and group records efficiently. Remember to test and debug your search views to ensure they work seamlessly. With these skills, you can build more flexible and user-friendly Odoo applications.

Feel free to adapt these techniques to fit your specific requirements and enhance your Odoo development skills further!

Make a Comment

Your email address will not be published. Required fields are marked *