In this tutorial, we explore how to add the group by option in an Odoo list view. We explain each step and show code examples so you can implement and test a grouped list view on your own Odoo instance. Moreover, we describe how to modify the search view and XML files to include grouping options that enhance user experience in managing records.
Introduction to Grouping in Odoo List Views
Firstly, Odoo Group By Option you should understand that grouping records in list views improves data organization. Additionally, grouping allows users to filter and manage records based on common field values. Consequently, you can save time and increase clarity when viewing hundreds of records. Moreover, Odoo provides built-in features to add a group by dropdown in the list view, and you can extend these options with custom XML definitions and Python methods.
Why Use Group By in Odoo?
Initially, grouping improves readability and efficiency. Furthermore, it allows users to toggle between grouped and non-grouped views with a simple click. In addition, when you group records, related fields appear together so that you can quickly scan through the data. Therefore, by integrating the group by option into your list view, you help users navigate large datasets with ease.
Understanding the Architecture of Odoo Views
Next, you need to know that Odoo Group By Option user interface relies on XML definitions to generate views. Moreover, the integration between XML views and Python models enables seamless functionality. For example, a list view (or tree view) is defined in XML to list records and can contain buttons for common actions. Consequently, you can extend these XML configurations to include group by options, which are usually added in the search view.
Adding Group By Options to Your List View
Then, you need to modify both the search view and list view XML files to add the grouping functionality in Odoo. Additionally, this tutorial explains how to add a group by option, modify the file text upload, and adjust the Python method if needed.
Step 1: Modifying the XML for the Search View
Firstly, you add options in the search view to allow grouping by a specific field. Consequently, when a user clicks on the group by button, the list view reorganizes the records accordingly. To do this, you will create or modify an XML file that defines the search view.
For example, you can design your search view as shown below:
<record id="view_student_search" model="ir.ui.view">
<field name="name">wb.student.search</field>
<field name="model">wb.student</field>
<field name="arch" type="xml">
<search string="Search Students">
<field name="school_id"/>
<filter string="Group by School" context="{'group_by':'school_id'}"/>
<filter string="Group by Year" context="{'group_by':'year'}"/>
</search>
</field>
</record>
Here, you make use of the filter element and add a context attribute that passes the group_by option to the list view. Moreover, you can add more filters as needed. Thus, you provide several options for users to group the records based on different criteria.
Step 2: Updating the List View XML File
Next, you must update the list view XML file to ensure it accepts the group by options from the search view. In Odoo, a list view is typically defined with the <tree> tag. To illustrate, consider the following XML snippet:
<record id="wb_student_tree_view" model="ir.ui.view">
<field name="name">wb.student.list.view</field>
<field name="model">wb.student</field>
<field name="arch" type="xml">
<tree string="Student" decoration-bf="id == 1" decoration-info="id == 1" decoration-warning="id == 1" decoration-danger="id == 1" decoration-primary="id == 1" decoration-muted="id == 1" decoration-success="id == 1">
<field name="id"/>
<field name="school_id"/>
<group>
<group name="school_id">
<button type="edit" name="edit" icon="fa-edit" title="Edit"/>
<button type="object" name="abc_test" string="Click Here"/>
</group>
</group>
</tree>
</field>
</record>
In this code, every field is listed so that when the group by option is activated, the records are correctly arranged. Moreover, thanks to the link between the search view and list view, you can instantly see grouped records when the user selects a group option.

Step 3: Creating a Custom Python Method (Optional)
Then, you can add a Python method for extra operations if needed. Although the group by function mainly works through XML, you might use a method to process answers or for testing purposes. For instance, you can create a simple method that prints information when the “Click Here” button is pressed.
from odoo import models, fields, api
class School(models.Model):
_name = 'school'
_description = 'School'
name = fields.Char(string="School Name")
year = fields.Integer(string="Year")
def action_test(self):
# This method prints the record that triggered the action.
print("Test action triggered for:", self.name)
In this example, the action_test method integrates with the XML button and prints debug information. Furthermore, the code uses an active command to show the functionality immediately. Additionally, you could extend the method to interact with the group by functionality if needed.
Testing and Deploying Your Customized List View
Subsequently, you must test your configuration on a development instance of Odoo. Therefore, first update the module, reload the views, and then click on the search view dropdown. Moreover, you should verify that the grouping works based on the selected field.
Step 4: Installation and Verification
Initially, you install your custom module. Then, you reload the module list in Odoo and upgrade your module. After that, you open the student list view and check the group by options. Consequently, you see records automatically group by the chosen fields such as School or Year. Therefore, testing is essential to verify that the XML and Python code integrate correctly.
You can also enable developer mode to observe detailed logs and check for any XML errors. Also, use the Odoo debugger to inspect the context data that triggers the grouping. Thus, you achieve a smooth transition from configuration to execution.
Common Issues and How to Fix Them
Next, you might encounter a few common issues. For instance, if the group by options do not appear, you must ensure that the search view XML has the proper context keys. Additionally, the list view must declare the fields properly so that these options take effect. Furthermore, you should check if the module has been upgraded correctly after modifications. Therefore, always clear the cache and refresh your browser to see changes.
Moreover, you can refer to the Odoo Documentation for more details on the file text upload and view customization. Hence, troubleshooting is easier when you trust the official sources.
Best Practices for Implementing Group By Options
Then, you must follow best practices to ensure that your module remains maintainable and efficient over time. Additionally, you should use consistent naming conventions and document your code. Consequently, future developers will understand your modifications and be able to extend them easily.
Use Clear and Consistent Field Names
Initially, you use names like school_id and year to indicate grouping fields. Also, use intuitive names for records and fields so that other developers know what data they represent. Moreover, you keep the naming conventions uniform through your XML and Python code. Thus, this best practice improves readability and maintainability.
Include Detailed Comments
Next, you add documentation within your XML and Python files. In addition, you comment on why specific group by options are added. Furthermore, you note the role of each element, which simplifies debugging and assists collaborators in the future. Therefore, measure to document your code thoroughly.
Integrate External Documentation
Finally, you add external links such as Odoo Documentation to help users learn more about view inheritance and customizations. Also, you might include links to Odoo community forums and GitHub repositories. Consequently, your readers gain access to comprehensive resources to deepen their understanding.
Practical Example Walkthrough
Now, you will review a practical example. Here, you integrate the search view and list view modifications, and then you run the custom Python method. This walkthrough shows how to combine all components to achieve a grouped list view. Moreover, it demonstrates how easy it is to rearrange records with the group by feature.
Example Recap: XML Code
Firstly, you observe the modified search view XML:
<record id="view_student_search" model="ir.ui.view">
<field name="name">wb.student.search</field>
<field name="model">wb.student</field>
<field name="arch" type="xml">
<search string="Search Students">
<field name="school_id"/>
<filter string="Group by School" context="{'group_by':'school_id'}"/>
<filter string="Group by Year" context="{'group_by':'year'}"/>
</search>
</field>
</record>
Then, you examine the customized list view XML:
<record id="wb_student_tree_view" model="ir.ui.view">
<field name="name">wb.student.list.view</field>
<field name="model">wb.student</field>
<field name="arch" type="xml">
<tree string="Student" decoration-bf="id == 1" decoration-info="id == 1" decoration-warning="id == 1" decoration-danger="id == 1" decoration-primary="id == 1" decoration-muted="id == 1" decoration-success="id == 1">
<field name="id"/>
<field name="school_id"/>
<group>
<group name="school_id">
<button type="edit" name="edit" icon="fa-edit" title="Edit"/>
<button type="object" name="abc_test" string="Click Here"/>
</group>
</group>
</tree>
</field>
</record>
Lastly, you review the sample Python code that supports the functionality:
from odoo import models, fields, api
class School(models.Model):
_name = 'school'
_description = 'School'
name = fields.Char(string="School Name")
year = fields.Integer(string="Year")
def action_test(self):
# This function is triggered when the user clicks the button in tree view.
print("Test action triggered for:", self.name)
In this example, every step uses the active voice and clear instructions. Also, you use transition words such as “firstly,” “next,” and “finally” to guide the reader.
Conclusion
In conclusion, you have learned how to add a group by option in a list view based on group fields in an Odoo view. Additionally, you saw how to modify the search view and list view XML files and integrate them with Python methods. Moreover, you applied best practices and documentation to make the code easily maintainable. Consequently, these skills empower you to build interactive and user-friendly modules.
Furthermore, you are encouraged to experiment with these examples. Therefore, try to group by other fields and test the system in your own Odoo instance. Also, you can visit the Odoo Documentation for more detailed information about view inheritance and customization.
Finally, if you have any questions or ideas, please leave a comment below. Also, share your thoughts on how grouping has improved your workflow. Ultimately, your feedback can inspire new tutorials and feature enhancements. Hence, thank you for reading this tutorial, and I look forward to seeing you in the next session where we discuss how to add headers in list view displays.
Until then, happy coding and see you next time!
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.

