Skip to content

Master Odoo 18 Default Filter Group: Essential Steps for Enhanced Data Management

odoo 18 default filter group

Are you looking to significantly boost productivity and streamline data navigation within your Odoo 18 environment? One of the most powerful yet often underutilized features in Odoo is the ability to set a Odoo 18 default filter group for your search views. Imagine users opening a list of sales orders, and it’s already filtered by ‘Draft’ orders and grouped by ‘Customer’ – instantly providing actionable insights without a single click. This isn’t just a convenience; it’s a strategic advantage for efficient business operations.

This comprehensive guide will walk you through the precise steps to configure these vital defaults, ensuring your team spends less time searching and more time achieving. We’ll dive deep into the XML definitions required, explaining each component with clarity, and providing a step-by-step tutorial to implement this functionality in your custom Odoo modules.

This article builds upon the excellent insights shared by Malek Hajmohammadi. You can find more of his valuable content on this topic here: Set Default Filter / Group by in a Search View.

Understanding Search Views: The Foundation of Data Navigation in Odoo 18

Before we configure an Odoo 18 default filter group, it’s essential to grasp the core concepts of Odoo’s search views. In Odoo, search views are the primary mechanism through which users interact with lists of records, providing powerful tools to organize and analyze data. They are comprised of two main components: filters and group-by options.

Filters serve to narrow down the displayed records based on specific criteria. For instance, in a list of sales orders, you might want to see only ‘Draft’ orders or those placed within the ‘Last 30 Days’. Filters provide these quick-access options, allowing users to isolate relevant data with ease.

Grouping allows users to categorize records based on a selected field. Instead of just a flat list of sales orders, you could group them by ‘Customer’ to see all orders associated with each client, or by ‘Order Date’ to analyze sales trends over time. This aggregation is incredibly useful for summarizing data and identifying patterns, offering a more structured and insightful view of your business information.

The proper configuration of these search view elements is critical for a smooth user experience. When you effectively utilize the Odoo 18 default filter group functionality, you’re not just setting a default; you’re designing a streamlined pathway for your users to access the most pertinent information right from the start.

Defining Filters in Your Odoo 18 Search View: A Step-by-Step Guide

The first step in establishing an Odoo 18 default filter group is to define the individual filters that will be available in your search view. These are created using the <filter> tag within the <search> tag in your XML definition. Let’s use the sale.order model as our example. We’ll define filters for different order statuses and a dynamic date range.

XML Code for Filter Definition:

<record id="view_sale_order_filter" model="ir.ui.view">
    <field name="name">sale.order.search</field>
    <field name="model">sale.order</field>
    <field name="arch" type="xml">
        <search>
            <filter string="Draft" domain="[('state', '=', 'draft')]"/>
            <filter string="Sent" domain="[('state', '=', 'sent')]"/>
            <filter string="Sale Orders" domain="[('state', '=', 'sale')]"/>
            <filter string="Last 30 Days"
                    domain="[('date_order', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
        </search>
    </field>
</record>

Explanation of Filter Elements:

  • <record id="view_sale_order_filter" model="ir.ui.view">: This line defines a new XML record. id provides a unique identifier for this specific view (crucial for referencing it later), and model="ir.ui.view" tells Odoo that this record defines a user interface view.
  • <field name="name">sale.order.search</field>: A descriptive internal name for the view.
  • <field name="model">sale.order</field>: Specifies which Odoo model this search view applies to. In this case, it’s the sale.order model.
  • <field name="arch" type="xml">: This field holds the actual XML architecture of the view.
  • <search>: The parent tag for all filter and group-by definitions within a search view.
  • <filter string="Draft" domain="[('state', '=', 'draft')]"/>:
    • string: This is the human-readable label that appears as a filter button in the Odoo UI.
    • domain: This is the most critical part. It’s a list of tuples defining the filtering condition. [('state', '=', 'draft')] instructs Odoo to only show records where the state field is exactly ‘draft’.
  • <filter string="Last 30 Days" domain="[('date_order', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>: This demonstrates a powerful dynamic filter.
    • The domain uses a Python expression to calculate a date 30 days prior to the current day (context_today() - timedelta(days=30)). This ensures the filter is always up-to-date, filtering sales orders placed within the last 30 days.

By clearly defining these filters, you lay the groundwork for a highly functional search interface. These filters can then be activated by default as part of your Odoo 18 default filter group.

Unleashing Data Insights with Default Grouping in Odoo 18

Beyond filtering, grouping records offers a profound way to structure and analyze your data. When you configure an Odoo 18 default filter group, you can also specify how records should be grouped automatically. This feature is defined using the <group> tag, and the context attribute within its child <filter> tags.

XML Code for Grouping Definition:

<record id="view_sale_order_group_by" model="ir.ui.view">
    <field name="name">sale.order.search.group.by</field>
    <field name="model">sale.order</field>
    <field name="arch" type="xml">
        <search>
            <filter string="Draft" domain="[('state', '=', 'draft')]"/>
            <filter string="Sent" domain="[('state', '=', 'sent')]"/>
            <filter string="Sale Orders" domain="[('state', '=', 'sale')]"/>
            <filter string="Last 30 Days"
                    domain="[('date_order', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
            <group expand="1" string="Group By">
                <filter string="Customer" domain="[]" context="{'group_by': 'partner_id'}"/>
                <filter string="Order Date" domain="[]" context="{'group_by': 'date_order'}"/>
            </group>
        </search>
    </field>
</record>

Explanation of Grouping Elements:

  • <group expand="1" string="Group By">:
    • expand="1": This attribute dictates that the “Group By” section should be expanded by default in the UI, making the grouping options immediately visible.
    • string="Group By": This is the label for the grouping section in the search view.
  • <filter string="Customer" domain="[]" context="{'group_by': 'partner_id'}"/>:
    • string="Customer": The label for this specific grouping option.
    • domain="[]": For group-by filters, the domain is often empty because these filters don’t restrict the records shown; they only organize them.
    • context="{'group_by': 'partner_id'}": This is the key. It tells Odoo to group the displayed records by the partner_id field, which typically represents the customer in the sale.order model.
  • <filter string="Order Date" domain="[]" context="{'group_by': 'date_order'}"/>: Similarly, this option allows users to group records by their date_order field.

By adding these grouping options, you empower users to quickly switch between different aggregated views. When combined with an Odoo 18 default filter group through actions, this becomes an incredibly potent tool for data analysis.

The Power of Automation: Setting the `Odoo 18 Default Filter Group` via Actions

Defining filters and group-by options is only half the battle. To truly leverage the power of the Odoo 18 default filter group, you need to configure an action that automatically applies these settings when a user accesses a particular view. This is achieved by modifying the ir.actions.act_window record associated with your model.

The context field within the window action is where the magic happens, allowing you to pass default values and settings to the view.

XML Code for Action Definition:

<record id="action_sale_order" model="ir.actions.act_window">
    <field name="name">Sales Orders</field>
    <field name="res_model">sale.order</field>
    <field name="view_mode">tree,form</field>
    <field name="search_view_id" ref="view_sale_order_filter"/>
    <field name="context">{'search_default_draft': 1, 'group_by': 'partner_id'}</field>
</record>

Explanation of Action Elements:

  • <record id="action_sale_order" model="ir.actions.act_window">: This defines a window action, which controls how a specific model’s data is presented in the UI.
  • <field name="name">Sales Orders</field>: The display name for this action (e.g., how it appears in menus).
  • <field name="res_model">sale.order</field>: Specifies the target model for this action.
  • <field name="view_mode">tree,form</field>: Defines the available display modes for this action (list view and form view).
  • <field name="search_view_id" ref="view_sale_order_filter"/>: This is crucial! It links this action to the specific search view you defined earlier. In our example, it references view_sale_order_filter, which contains both the filter and grouping definitions (though you could reference view_sale_order_group_by if you prefer that explicit grouping view).
  • <field name="context">{'search_default_draft': 1, 'group_by': 'partner_id'}</field>: This is where you set the `Odoo 18 default filter group`.
    • 'search_default_draft': 1: This line tells Odoo to activate the filter whose string attribute is “Draft” (converted to lowercase and spaces replaced by underscores, i.e., draft) by default when this action is loaded. The 1 signifies “true” or “active”.
    • 'group_by': 'partner_id': This automatically applies a grouping by the partner_id field. When users open the Sales Orders view through this action, they will immediately see records grouped by customer.

By defining the Odoo 18 default filter group within the action’s context, you create a truly seamless and intuitive user experience. This level of customization ensures that data is presented in the most relevant and organized way possible, right from the moment of access.

Bringing It All Together: Implementing Your `Odoo 18 Default Filter Group` in a Custom Module

To make these configurations active in your Odoo 18 instance, you’ll need to encapsulate them within a custom Odoo module. This provides a structured and maintainable way to manage your customizations.

Step-by-Step Module Creation:

  1. Create a New Odoo Module:
    • Start by creating a new directory in your Odoo addons path (e.g., my_sale_enhancements).
    • Inside this directory, create an __init__.py file (which can be empty) and a __manifest__.py file.
    • For a detailed guide on creating modules, refer to Odoo’s official documentation or a community tutorial like [Creating Your First Odoo Module](https://www.odoo.com/documentation/18.0/developer/howtos/backend.html).
  2. Define Your Module’s Manifest (__manifest__.py):
    • This file provides metadata about your module and specifies its dependencies and data files.
    
    {
        'name': 'My Sale Enhancements',
        'version': '1.0',
        'depends': ['sale'], # Important: Your module depends on the 'sale' module
        'data': [
            'views/sale_views.xml', # Reference your XML file here
        ],
        'installable': True,
        'application': False,
        'auto_install': False,
        'license': 'LGPL-3',
    }
    
  3. Create Your Views Directory and XML File:
    • Inside your my_sale_enhancements module directory, create a subdirectory named views.
    • Inside the views directory, create an XML file (e.g., sale_views.xml).
  4. Place the XML Code Snippets:
    • Combine all the XML code snippets for your search views (view_sale_order_filter and view_sale_order_group_by) and the window action (action_sale_order) into this sale_views.xml file. Ensure they are wrapped within the standard Odoo <odoo> and <data> tags.
    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
            <!-- Search View with Filters -->
            <record id="view_sale_order_filter" model="ir.ui.view">
                <field name="name">sale.order.search</field>
                <field name="model">sale.order</field>
                <field name="arch" type="xml">
                    <search>
                        <filter string="Draft" domain="[('state', '=', 'draft')]"/>
                        <filter string="Sent" domain="[('state', '=', 'sent')]"/>
                        <filter string="Sale Orders" domain="[('state', '=', 'sale')]"/>
                        <filter string="Last 30 Days"
                                domain="[('date_order', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
                    </search>
                </field>
            </record>
    
            <!-- Search View with Grouping Options (can be merged with above if desired) -->
            <!-- For simplicity and clear explanation, we keep it separate as per context, 
                 but note that search_view_id will only refer to one. -->
            <record id="view_sale_order_group_by" model="ir.ui.view">
                <field name="name">sale.order.search.group.by</field>
                <field name="model">sale.order</field>
                <field name="arch" type="xml">
                    <search>
                        <filter string="Draft" domain="[('state', '=', 'draft')]"/>
                        <filter string="Sent" domain="[('state', '=', 'sent')]"/>
                        <filter string="Sale Orders" domain="[('state', '=', 'sale')]"/>
                        <filter string="Last 30 Days"
                                domain="[('date_order', '>=', (context_today() - timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
                        <group expand="1" string="Group By">
                            <filter string="Customer" domain="[]" context="{'group_by': 'partner_id'}"/>
                            <filter string="Order Date" domain="[]" context="{'group_by': 'date_order'}"/>
                        </group>
                    </search>
                </field>
            </record>
    
            <!-- Window Action with Default Filter and Grouping -->
            <record id="action_sale_order" model="ir.actions.act_window">
                <field name="name">Sales Orders</field>
                <field name="res_model">sale.order</field>
                <field name="view_mode">tree,form</field>
                <field name="search_view_id" ref="view_sale_order_filter"/>
                <field name="context">{'search_default_draft': 1, 'group_by': 'partner_id'}</field>
            </record>
        </data>
    </odoo>
    
  5. Install and Test Your Module:
    • Restart your Odoo server (if running).
    • Navigate to the Apps menu in Odoo, update the apps list, and then search for “My Sale Enhancements”.
    • Click “Install”.
    • Once installed, go to the Sales module and open the Sales Orders view.
    • Verify that:
      • The “Draft” filter is automatically applied to the list.
      • The records are grouped by customer (partner_id).

Advanced Tips for Your Odoo 18 Default Filter Group

Implementing a basic Odoo 18 default filter group is a great start, but Odoo offers even more flexibility for advanced use cases.

  • Combining Multiple Default Filters: You can activate multiple default filters by adding more search_default_KEY: 1 entries to your action’s context. For example, {'search_default_draft': 1, 'search_default_last_30_days': 1} would show draft orders from the last 30 days.
  • Conditional Grouping: While group_by in the action’s context sets a single default, users can still change grouping. For very specific reporting needs, consider creating multiple actions that lead to the same model but with different `Odoo 18 default filter group` and grouping contexts.
  • Performance Considerations: Be mindful of overly complex domain expressions, especially those involving many-to-many relationships or computationally intensive date calculations on very large datasets. Test performance thoroughly.
  • User Experience Best Practices: While setting defaults is excellent, ensure they align with the most common user workflows. Overly restrictive defaults can be frustrating. Provide clear labels and empower users to easily modify or clear filters/groups if needed.
  • Security: Always consider Odoo’s security rules. If your filters depend on fields that users don’t have access to, they might not work as expected. Ensure your module adheres to proper access rights.

Troubleshooting Common Issues

Even with careful implementation, you might encounter issues. Here are some common troubleshooting tips for your Odoo 18 default filter group:

  • Filter/Grouping Not Applying:
    • Check __manifest__.py: Ensure your XML file (views/sale_views.xml) is correctly listed in the data array and that depends includes the necessary modules (e.g., 'sale').
    • Module Update: After any XML changes, remember to upgrade your module in Odoo (Apps > search your module > Upgrade).
    • XML Syntax Errors: Even a small typo can break the entire view. Check your Odoo server logs for XML parsing errors.
    • Incorrect id Reference: Verify that search_view_id in your action correctly references the id of your search view.
    • Incorrect context Key: For default filters, the key in context must be search_default_ followed by the lowercase, underscore-separated string of your filter (e.g., search_default_last_30_days for a filter with string="Last 30 Days"). For grouping, ensure it’s group_by: 'field_name'.
  • Field Not Found Errors: Double-check that the field names used in your domain (e.g., state, partner_id, date_order) truly exist on the sale.order model.
  • Conflicting Views: If you have multiple search views or actions targeting the same model, ensure they don’t override each other unintentionally. Use unique ids for all your records.

By systematically checking these points, you can quickly diagnose and resolve most issues related to configuring your Odoo 18 default filter group.

Conclusion

Mastering the configuration of an Odoo 18 default filter group is an invaluable skill for any Odoo developer or administrator. It transforms raw data lists into organized, actionable insights, significantly improving user experience and overall operational efficiency. By following this detailed, step-by-step tutorial, you’ve gained the knowledge to implement predefined filters and grouping options, automating data organization and empowering your team to work smarter, not harder.

Don’t let your users waste time on repetitive clicks. Take control of your Odoo views and propel your business forward with intelligent, pre-configured data displays. Implement these changes today and witness a remarkable boost in productivity and data management prowess within your Odoo 18 environment!


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Leave a Reply

WP Twitter Auto Publish Powered By : XYZScripts.com