Filters, Group By & Dynamic Search
Firstly, Odoo 18 search views let you build powerful filters, flexible group-by options, and dynamic search panels that match your exact needs. Moreover, this tutorial uses clear examples and code snippets to guide you step by step. Additionally, you’ll see how to customize your XML search view records, test them in the UI, and optimize performance. Consequently, by following this guide, you’ll master Odoo 18 search views and boost your users’ productivity from day one.
Why Odoo 18 search views Matter
Furthermore, Odoo’s search interface sits at the heart of every list or kanban view. Therefore, you should craft it carefully. In addition, well-designed search views help users find records instantly, group data meaningfully, and apply dynamic filters without extra code. Finally, by using search views effectively, you reduce support tickets and improve user satisfaction.
Prerequisites & Setup
Moreover, before you begin, ensure that you have:
- Odoo 18 installed and running locally or on your server
- Basic knowledge of Python and XML
- A custom module scaffold where you can add your view files
Additionally, you may refer to the official Odoo documentation for more on module structure and view types.
How to Configure Odoo 18 search views
Firstly, every search view lives in an XML file under your module’s views/
folder. Secondly, you must create an <record>
with model="ir.ui.view"
and a <search>
root tag. Then, you add <filter>
and <group>
tags inside. Next, you include any <field>
tags to let users search on those fields directly.
Defining a Basic Search View
Step 1: Create the Search View Record
Moreover, start by declaring your view in views/res_partner_search.xml
:
xmlCopyEdit<odoo>
<record id="view_res_partner_search" model="ir.ui.view">
<field name="name">res.partner.search</field>
<field name="model">res.partner</field>
<field name="arch" type="xml">
<search string="Partners">
<!-- filters, groups, and fields go here -->
</search>
</field>
</record>
</odoo>
Furthermore, note that the <search>
tag’s string
attribute sets the panel’s title.
Step 2: Add Filter Elements
Next, you can let users narrow records by adding <filter>
elements:
xmlCopyEdit<filter
name="filter_customers"
string="Customers"
domain="[('customer_rank', '>', 0)]"/>
<filter
name="filter_vendors"
string="Vendors"
domain="[('supplier_rank', '>', 0)]"/>
Additionally, each filter needs a unique name
, a display string
, and a domain
—a standard Odoo domain list.
Step 3: Insert Group By Options
Moreover, grouping lets users aggregate by a field. Then, wrap group options in a <group>
tag:
xmlCopyEdit<group expand="0" string="Group By">
<filter string="Country" context="{'group_by':'country_id'}"/>
<filter string="Salesperson" context="{'group_by':'user_id'}"/>
</group>
Furthermore, the expand="0"
attribute collapses the list by default, and the inner filters use context
to set the group_by
key.
Step 4: Expose Searchable Fields
Next, you add <field>
tags to let users type keywords:
xmlCopyEdit<field name="name"/>
<field name="email"/>
<field name="country_id"/>
Additionally, Odoo will generate corresponding input boxes or dropdowns based on field types.
Complete Example: Partners Search View
xmlCopyEdit<odoo>
<record id="view_res_partner_search" model="ir.ui.view">
<field name="name">res.partner.search</field>
<field name="model">res.partner</field>
<field name="arch" type="xml">
<search string="Partners">
<filter
name="filter_customers"
string="Customers"
domain="[('customer_rank', '>', 0)]"/>
<filter
name="filter_vendors"
string="Vendors"
domain="[('supplier_rank', '>', 0)]"/>
<group expand="0" string="Group By">
<filter string="Country" context="{'group_by':'country_id'}"/>
<filter string="Salesperson" context="{'group_by':'user_id'}"/>
</group>
<field name="name"/>
<field name="email"/>
<field name="country_id"/>
</search>
</field>
</record>
</odoo>
Advanced Filters in Odoo 18 search views
Furthermore, you can craft more complex filters—such as date ranges or dynamic domains—by using Python expressions or context variables.
Filtering by Date Range
Moreover, to let users pick “This Month” or “Last Year,” add filters with evaluated domains:
xmlCopyEdit<filter
name="filter_this_month"
string="This Month"
domain="[
('create_date','>=', (context_today().replace(day=1))),
('create_date','<=', context_today())
]"/>
<filter
name="filter_last_year"
string="Last Year"
domain="[
('create_date','>=', (context_today().replace(year=context_today().year-1, month=1, day=1))),
('create_date','<=', (context_today().replace(year=context_today().year-1, month=12, day=31)))
]"/>
Additionally, these use the context_today()
helper to compute dates at runtime.
Dynamic Search Based on Current User
Furthermore, to let each user quickly find their own records, define:
xmlCopyEdit<filter
name="filter_my_records"
string="My Records"
domain="[('user_id','=', uid)]"/>
Moreover, uid
is the logged-in user’s ID, and Odoo fills it automatically.
Group By Advanced Options in Odoo 18 search views
Additionally, you can nest group-by fields or allow multiple grouping levels.
Nested Grouping
Moreover, allow users to select subgroups by nesting <group>
tags:
xmlCopyEdit<group string="Advanced Group By" expand="0">
<group string="Primary">
<filter string="Country" context="{'group_by':'country_id'}"/>
</group>
<group string="Secondary">
<filter string="Salesperson" context="{'group_by':'user_id'}"/>
</group>
</group>
Furthermore, this gives a two-tier grouping interface in the UI.
Testing Your Odoo 18 search views
First, restart your Odoo server and clear the cache by upgrading your custom module:
bashCopyEditodoo -u your_module_name -d your_database
Next, navigate to the relevant menu—such as Contacts → Partners—to see your new search panel. Then, test each filter and group option to ensure domains and contexts work as expected.
Deploying Search Views in Production
Moreover, when you deploy to production:
- Commit your XML files into version control.
- Run migrations with
-u
. - Monitor logs for domain or rendering errors.
- In addition, consider adding automated tests for critical search domains.
Best Practices for Odoo 18 search views
Furthermore, keep these tips in mind:
- Name filters clearly. Then, users understand options immediately.
- Limit filter count. Additionally, avoid overwhelming the UI with too many choices.
- Use context sparingly. Moreover, context only when grouping or dynamic defaults.
- Optimize domains. Consequently, use indexed fields to speed up queries.
- Document custom views. Finally, add comments in XML so teammates follow your logic.
Sample: Search View for Sales Orders
Furthermore, here’s a quick example for sale.order
:
xmlCopyEdit<odoo>
<record id="view_sale_order_search" model="ir.ui.view">
<field name="name">sale.order.search.custom</field>
<field name="model">sale.order</field>
<field name="arch" type="xml">
<search string="Sales Orders">
<filter name="so_draft" string="Draft" domain="[('state','=','draft')]"/>
<filter name="so_confirmed" string="Confirmed" domain="[('state','=','sale')]"/>
<group string="Group By" expand="1">
<filter string="Customer" context="{'group_by':'partner_id'}"/>
<filter string="Salesperson" context="{'group_by':'user_id'}"/>
</group>
<field name="name"/>
<field name="date_order"/>
<field name="state"/>
</search>
</field>
</record>
</odoo>
Troubleshooting Common Issues
Moreover, if you see no filters:
- First, ensure your XML is valid. Next, check that the
model
matches. Then, clear the browser cache. - Furthermore, if domains error out, verify Python expressions are wrapped in lists properly.
- Additionally, use Odoo’s debug mode (
?debug=assets
) to view rendered XML in the debugger.
Summary & Next Steps
Finally, you’ve seen how to build Odoo 18 search views with custom filters, advanced group-by options, and dynamic search panels. Moreover, you’ve learned best practices and saw full code examples for partners and sales orders. Consequently, you can now tailor search interfaces to any model in your database. Therefore, experiment with date filters, user-based domains, and nested groupings to deliver a top-notch user experience.
Further Reading
Moreover, for more details on search views and available attributes, see the Odoo 18 Developer Reference.
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.