Skip to content
Home » My Blog Tutorial » Odoo 17 XML Search View: Adding Filters Tutorial

Odoo 17 XML Search View: Adding Filters Tutorial

Odoo search view

In our Odoo search view tutorial, we explore how to configure an XML search panel in Odoo 17 that displays records accurately and improves filter configuration. In this post, we explain how to correct XML syntax errors and add powerful filters using key phrases such as Odoo search view, XML configuration, filter setup, and Odoo 17. We explain every step in detail and share code that you can copy, test, and customize for your module. You also find an outgoing link to the Odoo official documentation for further insights.

Introduction

In this tutorial we teach you how to add a search panel in Odoo search view with proper XML configuration. We start by discussing the basis of an Odoo search panel and then advance to correcting common XML syntax errors. We focus on active filter usage, proper date formatting, and structuring advanced filtering, so that you enjoy smooth functionality in your Odoo 17 modules. You will learn to add filters such as date filters, gender-based filters, and dynamic keywords with real-world examples. Moreover, we include code samples in a clear step-by-step manner to save your time while debugging XML files.

We use key phrases like Odoo search view and XML configuration for Odoo 17 throughout the post so that you can easily follow and relate to the specific improvements that your Odoo module might need. We also recommend exploring alternative solutions through the Odoo official documentation.


H2: Understanding the Odoo Search View and XML Configuration

Before applying any changes, you must understand how the Odoo search view works. Odoo search views are defined using XML and allow you to filter and group records easily. In other words, you use XML to configure a search panel that interacts with your models without writing extra Python code.

H3: Overview of Search Panel Functionality

The search panel in Odoo displays fields and filters that help end users refine their records. When you define filters like “Last 7 Days” or “Male Students,” you give periodic results that are computed at runtime. The active use of keywords such as create_date, filter, and domain in your XML helps Odoo find records quickly.

For example, consider the code below that shows a simple search view configuration with date filtering:

<search>
  <field name="create_date" string="Create Date" date="create_date"/>
  <filter string="Last 7 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]" />
</search>

In this snippet, you define a field named create_date and attach a filter that returns records created in the last seven days. This snippet uses key phrases such as Odoo search view and filter configuration for Odoo 17. You must adapt your XML code as shown below so that your search view displays the desired data accurately.

H3: Common XML Syntax Errors in Odoo 17

Many developers encounter syntax errors when configuring the Odoo search view. Errors such as unclosed tags, mismatched attributes, and incomplete domains often occur. Therefore, you must check every XML tag carefully. Use clear naming, proper attribute formatting, and ensure that every opening tag has a closing tag. In our experience, transitioning to correct XML syntax drastically reduces module crashes and enhances user experience.

Transitioning to multi-option filters and ensuring active domains will enhance your workflow. Additionally, you should test your XML files by updating the module and verifying if the search view renders properly.


H2: Step-by-Step Guide for Building a Custom Search Panel

In this section you will learn to build a custom search view step by step. We use clear code examples and clear language with active verbs. Follow these instructions to integrate the search panel in your Odoo 17 module.

H3: Step 1 – Creating the Basic Search View Structure

First, you need to create a search view record by defining a new record node in your XML file. In the code snippet below, we correctly structure the search view with proper tags and attributes.

<record id="wb_student_search_view" model="ir.ui.view">
  <field name="name">wb.student.search.view</field>
  <field name="model">wb.student</field>
  <field name="arch" type="xml">
    <search>
      <field name="create_date" string="Create Date" date="create_date"/>
      <filter name="7_days" string="7 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>
      <filter name="14_days" string="14 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=14)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>
      <field name="name"/>
      <field name="school_id"/>
      <field name="gender"/>
      <separator/>
      <filter name="school_name" domain="[('school_id.name','ilike','sunny')]" string="School Sunny"/>
      <separator/>
      <filter name="student_name" domain="[('name', 'ilike', 'sunny')]" string="Sunny Student"/>
      <filter name="gender_male_filter" domain="[('gender','=','male')]" string="Male Students"/>
      <filter name="gender_female_filter" domain="[('gender','=','female')]" string="Female Students"/>
    </search>
  </field>
</record>

In this code, we define multiple filters that target different fields. You see that we use attributes like name, string, and domain in proper format. Notice that every tag is clearly closed, which prevents errors during module updates.

Using transition words such as “first,” “next,” and “finally” in your code and documentation helps you maintain a detailed step-by-step approach. You also use clear and simple language to explain key phrases like Odoo search view and filter configuration for Odoo 17 consistently throughout your XML code.

H3: Step 2 – Adding Advanced Filter Options

Next, you add advanced filter options so users filter records based on parameters such as gender or school names. By incorporating multi-option attributes and using domain filters, you enhance user experience. The following snippet demonstrates additional filters with a detailed explanation for each attribute:

<search>
  <!-- Date Filter Fields -->
  <field name="create_date" string="Create Date" date="create_date"/>
  <filter name="7_days" string="7 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>
  <filter name="14_days" string="14 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=14)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>

  <!-- Identifier Fields -->
  <field name="name"/>
  <field name="school_id"/>
  <field name="gender"/>
  <separator/>

  <!-- Specific Filters for School and Students -->
  <filter name="school_name" domain="[('school_id.name','ilike','sunny')]" string="School Sunny"/>
  <separator/>
  <filter name="student_name" domain="[('name', 'ilike', 'sunny')]" string="Sunny Student"/>

  <!-- Gender Filters -->
  <filter name="gender_male_filter" domain="[('gender','=','male')]" string="Male Students"/>
  <filter name="gender_female_filter" domain="[('gender','=','female')]" string="Female Students"/>
</search>

Each filter in the code above is active and self-explanatory. The commented sections guide other developers through the structure of filters, ensuring that every piece of information is immediately clear. Transition words such as “first” and “finally” guide the readers to the expected order of operations.

H3: Step 3 – Validating XML Syntax

After writing your XML file, you must validate the file to ensure it contains no syntax errors. During this process, you check whether each opening tag has a matching closing tag. You also verify that every attribute is enclosed in quotes and that every domain is properly formatted.

For example, if you mistakenly omit a closing tag or misformat a domain string, your module update will fail. Therefore, using an XML linter or your IDE to automatically check the syntax can significantly reduce time spent debugging.


H2: Troubleshooting Common Issues in Odoo XML Search Views

Even after following the instructions, you might encounter some problems. In this section, we describe common issues and provide solutions that use clear active sentences and transition words to guide you step by step.

H3: Issue 1 – Missing or Unclosed Tags

When you update your module and the search view does not render, you likely have missing or unclosed tags. You must check that every <search>, <record>, <filter>, and <field> tag is correctly closed. Furthermore, always verify that all XML attributes have proper quotes.

Solution: Examine your code line by line, and use tools such as XML validators available online. You can copy and paste your XML code into a validator and check for errors. Correct any mistakes by adding the proper closing tags and ensuring correct syntax.

H3: Issue 2 – Incorrect Domain Configuration

Sometimes the domain might be incorrectly formatted, which causes the filter to return no results. You must always use the correct Python function and formatting for domain expressions. For example, using the strftime function properly is important when filtering by dates.

Solution: Compare your domain expressions with the sample code we provided. Transition to using a standardized format such as:

domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"

This format works because it dynamically calculates the date and formats it correctly. Always ensure that you include the correct number of % symbols when using format strings in XML.

H3: Issue 3 – Multi-Option Filter Configuration

You might face issues when implementing multi-option filters. Some users find that when they choose a filter, they receive no results even though data is available. The problem often lies in how the domain expressions are defined or how the field attributes are declared.

Solution: Double-check that each filter uses logical operators correctly. Use simple expressions and test each filter one at a time. Transition by first implementing a simple filter, verifying it works, and then adding more advanced options step by step.


H2: Best Practices for Odoo Search View Development

Following best practices when developing your Odoo search view ensures long-term efficiency and maintainability of your code. In addition, you improve performance and future-proof your module. We list some best practices below.

H3: Use Clear and Concise Attribute Names

Always choose clear and descriptive names for fields and filters. For example, use create_date or gender_male_filter to easily identify the purpose of each field or filter. This habit not only improves readability but also helps in troubleshooting.

H3: Test Incrementally

You must test your XML file after each change. Instead of writing all filters at once, add one filter, update the module, and test its functionality. Transition from simple filters to advanced options gradually. This active approach minimizes errors and simplifies debugging.

H3: Document Your Code

Document your XML code using comments. You can leave inline comments in your XML to explain the purpose of filters and fields. For instance, add a comment above each filter like this:

<!-- Filter to fetch records from the last 7 days -->
<filter name="7_days" string="7 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>

This strategy helps you remember your logic when revisiting the code later and assists other developers in understanding your configuration.

H3: Use Consistent Indentation and Formatting

You must maintain a consistent indentation style throughout your XML file. Consistent formatting improves readability and reduces the likelihood of syntax errors. Transition to using your code editor’s auto-format feature to keep things organized.


H2: Advanced Topics for Odoo XML Search Views

Once you have mastered the basics, you can explore more advanced topics such as integrating multiple filters, adding dynamic field options, and combining search views with kanban or list views. This section offers advanced tips and code examples.

H3: Combining Filters with Dynamic Fields

You can combine several filters and fields to make your search view more powerful. For example, you can allow filtering by both date and string-based conditions. The following example shows how you add dynamic search fields:

<search>
  <field name="name"/>
  <field name="school_id"/>
  <field name="create_date" string="Create Date" date="create_date"/>
  <filter name="active_students" domain="[('active', '=', True)]" string="Active Students"/>
  <filter name="gender_male_filter" domain="[('gender','=','male')]" string="Male Students"/>
  <filter name="gender_female_filter" domain="[('gender','=','female')]" string="Female Students"/>
</search>

Here, the key phrase Odoo search view and its synonyms are used actively. You ensure every active filter works by combining different fields. Transition by testing each dynamic field individually.

H3: Applying Relativedelta for Date Filters

When you want to filter records created within a specific period, you must use Python’s relativedelta. This dynamic function calculates dates based on a relative time period. As seen in our earlier code examples, you apply context_today() minus a relativedelta of 7 or 14 days to get the range. This method keeps your filters up to date automatically.

H3: Integrating XML Search View with Other Views

You can integrate the XML search view into list or kanban views. This integration means that your XML configuration is reused as the user navigates between different record views. When you define the search view in the action window, ensure that you reference its external ID correctly:

<record id="action_student_list" model="ir.actions.act_window">
  <field name="name">Student List</field>
  <field name="res_model">wb.student</field>
  <field name="view_mode">tree,form</field>
  <field name="search_view_id" ref="module_name.wb_student_search_view"/>
</record>

This record links the search view with the student module. Consequently, users see the proper filters when they view the student list. Transition gradually by testing every integration step.


H2: In-Depth Explanation of the Provided Code

In this section we elaborate on each part of the XML search view code. We explain why every tag and attribute exists and how to modify them for customized behavior. This detailed explanation helps both beginners and experienced developers to understand the logic behind the configurations.

H3: The <record> Tag and Its Attributes

The <record> node creates a new view record in the system. You provide an id for unique identification and set the model to "ir.ui.view". Then you add several <field> tags to record the view’s name, model, and architecture. For example:

<record id="wb_student_search_view" model="ir.ui.view">
  <field name="name">wb.student.search.view</field>
  <field name="model">wb.student</field>
  <field name="arch" type="xml">
    <!-- XML content goes here -->
  </field>
</record>

Each attribute is active and clearly defined. Transitioning from basic fields to more complex structures is simple when you follow this pattern.

H3: The <search> Tag and Its Children

Inside the <arch> field, you create the <search> tag that contains all filter and field definitions. You add <field> tags to automatically include columns that are searchable. Then you add <filter> tags to define custom search criteria. For instance:

<search>
  <field name="create_date" string="Create Date" date="create_date"/>
  <filter name="7_days" string="7 Days" domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"/>
  <!-- more filters follow -->
</search>

Using active language such as “add” and “define” throughout the code emphasizes that every element is functional. Transition words guide the reader from one element to another, ensuring that no step is skipped.

H3: Understanding the Domain Attribute

The domain attribute in every <filter> tag is crucial. It defines which records the filter applies to. In our examples, we use Python expressions inside a string to calculate dates dynamically. The use of double percentage symbols in strftime ensures that the formatting is preserved in XML and does not conflict with other placeholders.

For example, the domain for the last 7 days filter is:

domain="[('create_date', '>=', (context_today() - relativedelta(days=7)).strftime('%%Y-%%m-%%d 00:00:00'))]"

This line actively calculates the date range that determines record visibility. Moreover, you can modify any condition to apply to other fields as needed. Transitioning to alternative possibilities is encouraged by testing similar expressions.

H3: The Role of Separators

Separators, defined by the <separator/> tag, break up the search view into logical sections. They actively improve the layout by grouping similar filters together. Using separators effectively results in a more user-friendly interface. For example, the code below separates filters related to dates from those related to identifiers:

<field name="name"/>
<field name="school_id"/>
<separator/>
<filter name="school_name" domain="[('school_id.name','ilike','sunny')]" string="School Sunny"/>

This division actively helps users by visually grouping related fields. Transitioning between sections becomes intuitive as users look for now segmented filters.


H2: Final Testing and Deployment

After you write and document your search view XML code, you must test it within the Odoo environment. Follow these active steps to finalize your implementation.

H3: Step 1 – Restart Odoo and Update the Module

Restart your Odoo server and update the module that contains your XML search view. This action forces Odoo to reload your XML file. To do so, run commands such as:

./odoo-bin -u module_name

This command actively updates the specified module and shows any syntax errors or warnings directly in the terminal.

H3: Step 2 – Verify the Search View in the User Interface

Log in to your Odoo instance and navigate to the corresponding view. Check that all filters display and work as expected. You must click each filter and test that the proper records appear. Transition from one filter to another and verify that dynamic date calculations return the correct data.

H3: Step 3 – Debug and Validate Any Issues

If you encounter issues, return to your XML file and review the code. Testing individual components one at a time helps identify which section might have errors. Use your browser’s developer tools and the Odoo log to locate problems quickly. Active debugging improves your future coding efficiency.


H2: Conclusion and Next Steps

In summary, this tutorial actively teaches you how to build and refine an Odoo search view in XML for Odoo 17. By summarizing the best practices and advanced topics, you now know how to configure filters, manage dynamic date ranges, and integrate search views with other views. We have emphasized key phrases such as Odoo search view, XML configuration for Odoo 17, and filter setup throughout this post.

Moving forward, you should:

  • Implement and customize the provided XML code.
  • Test each filter actively to ensure smooth performance.
  • Document and validate your code changes using an XML validator.

For more detailed information, refer to the Odoo official documentation and explore additional tutorials on integrating advanced search features.

This hands-on guide makes it easier for you to manage and maintain your Odoo modules, ensuring a smooth user experience. We encourage you to leave a comment below if you have further questions or need additional examples. Happy coding and best of luck with your Odoo projects!


By following this comprehensive tutorial, you now have a complete understanding of how to add a search panel in Odoo search view. Every step uses active sentences and smooth transitions so that your implementation process becomes efficient and simple. Enjoy enhancing your Odoo modules and share your experiences with the community!


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

Tags:

Leave a Reply

Optimized by Optimole
WP Twitter Auto Publish Powered By : XYZScripts.com

Discover more from teguhteja.id

Subscribe now to keep reading and get access to the full archive.

Continue reading