Enable Dark Mode!
how-to-create-a-cohort-view-in-odoo-18.jpg
By: Amrutha AM

How to Create a Cohort View in Odoo 18

Technical Odoo 18

Odoo offers a variety of views, such as list, form, tree, kanban, graph, pivot, calendar, and cohort. Among these, the cohort view is a powerful reporting tool used to analyze and track user behavior over time. It organizes data into rows and columns, where each cell represents a specific metric for a cohort during a given time period. The cohort view makes it easy to see changes and trends daily, weekly, monthly, or yearly.

To create a cohort view in Odoo, you first need to define a model with the necessary fields. Below is an example demonstrating how to set up a cohort view in Odoo:

How to Create a Cohort View in Odoo 18-cybrosys

To create a cohort view in Odoo, you need to define a model and a view. In this example, we’ll add a new model to the Employee module and create a menu item under the Configuration tab.

# -*- coding: utf-8 -*-
from odoo import models, fields
class EmployeeDetails(models.Model):
  _name = 'employee.details'
  _description = 'Employee Details'
  _rec_name = "employee_id"
  employee_id = fields.Many2one('hr.employee', string="Employee")
  phone= fields.Char(related="employee_id.work_phone", readonly=True)
  email= fields.Char(related="employee_id.work_email", readonly=True)
  date = fields.Date(string="Date", required=True)

We’ve created a new class called EmployeeDetails, which allows selecting employees from the hr.employee model. The phone and email fields are set as related fields, so they will automatically populate when an employee is selected. A required date field is also added, which will be used as the date_stop in the cohort view.

Next, we need to add access rights for this model. To do that, create a new directory with a CSV file for security settings. In the file, define the ID, name, and model ID for the access rule. Then, include this security file in the module’s __manifest__.py.

id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_employee_details,access.employee.details,model_employee_details,base.group_user,1,1,1,1

Before creating the cohort view, make sure to add “web_cohort” as a dependency in the __manifest__.py file.

Next, create an XML file to define the view. Start by creating an ir.actions.act_window record for the employee.details model. In this action, specify the view modes you need — for example, tree, form, and cohort. Then, assign it an ID like employee_details_action_menu, which will be used to link this action to a menu item.

<record id="employee_details_action_menu" model="ir.actions.act_window">
  <field name="name">Employee Details</field>
  <field name="res_model">employee.details</field>
  <field name="type">ir.actions.act_window</field>
  <field name="view_mode">list,form,cohort</field>
</record>

Now let's create the cohort view. Start by defining a record with a unique ID and set the model to ir.ui.view. Then, provide a name and specify the model as employee.details. Inside the cohort tag, set a title using the string attribute. Use create_date as the date_start, and for date_stop, use the date field from the model you just created. You can set the interval to day, week, month, or year, and in this example, the mode is set to churn.

<record id="employee_details_view_cohort" model="ir.ui.view">
  <field name="name">employee.details.view.cohort</field>
  <field name="model">employee.details</field>
  <field name="arch" type="xml">
      <cohort string="Analysis" date_start="create_date" date_stop="date" interval="day" mode="churn"/>
  </field>
</record>

Next, create a menu item for the model. In the <menuitem> tag, set the action to the ID of the record you defined earlier, and specify the parent to indicate where the menu should appear in the menu structure.

<menuitem id="employee_details_menu_root"
    name="Employee Details"
    action="employee_details_action_menu"
    parent="hr.menu_config_employee"
    sequence="20"/>

After installing the module, go to the Employee module. You’ll find the Employee Details menu under the Configuration tab.

How to Create a Cohort View in Odoo 18-cybrosys

Clicking the menu will open the model in a tree view, where you can add and manage records. The image below shows how the tree view looks for this model.

How to Create a Cohort View in Odoo 18-cybrosys

These are the records created in the module, which are visible in the list view. You can also see the list and cohort view icons in the image above. Multiple records can be added, and by clicking the cohort view icon, you can view a day-wise report of the records directly in the UI.

How to Create a Cohort View in Odoo 18-cybrosys

The above figure displays the cohort view in the Odoo 18 interface. This shows the result of successfully creating a cohort view for the model. I hope the steps explained above are clear and helpful in understanding how to set up and use the cohort view in Odoo 18.

To read more about How to Create a Cohort View in Odoo 16, refer to our blog How to Create a Cohort View in Odoo 16.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message