There are different types of views in Odoo like list, form, tree, kanban, graph, pivot, calendar, cohort, etc. In Odoo, the cohort view is a type of reporting view that allows you to analyze and track user behavior over time. The cohort view displays rows and columns. The intersection of a row and column shows a particular metric or value for that cohort in that specific period, and we can easily get records day-wise, week, month, and year.
To create a cohort view in Odoo, we need to define a model with the required fields. Here's an example of how you can create a cohort view in Odoo:
To create a cohort view needs models and views. Here create a new model in the employee module and add a menu action under the configuration tab. Below is python code for creating a new class in Odoo.
from odoo import fields, models
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")
email = fields.Char(related="employee_id.work_email")
date = fields.Date(string="Date", required=True)
Created a new class EmployeeDetails, and here we can select employees from hr.employee. The phone and email fields are set as related fields, so when we select employees, those fields will fill automatically. Added a date field and set it as required because we set that date as date_stop in cohort view.
Next, we need to give security for the above model. For that, create a new directory with a new csv file. Specify an id, name, and model id here. And give this security file in the manifest file.
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access.emp_details,access_emp_details,model_employee_details,base.group_user,1,1,1,1
Before creating a cohort view add “web_cohort” as a dependent module in the manifest file.
After this, we can create an xml file to create a view. First, we want to create ir.actions.act.window model with model name employee.details, and here we need to specify the view mode which type required, below mentioned the view as tree, form, and cohort. And set an id employee_details_action_menu to provide menu action for our model and give this id as action in the 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">tree,form,cohort</field>
</record>
Next, we can create a cohort view, first need to give an id for the record and specify the model as ir.ui.view. And give a field name and add the model as employee.details. After that, mention the desired string for cohort tag, set date_start as create date, then take the date_stop from the newly created module, and we can provide the interval as day, week, month and year, and here the mode is set as 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>
Then set the menu action for the model. In the below menu item tag, set action as the record id which is mentioned above, and set the parent where we want to show the menu item.
<menuitem id="employee_details_menu_root"
name="Employee Details"
action="employee_details_action_menu"
parent="hr.menu_config_employee"
sequence="20"/>
After this, install the module and open the employee module, and we can see the employee details menu under the configuration tab.
Click on the menu, it will open a new model in the tree view format, where we can create new records. The below image shows the tree view of the model.
These are the records we created in the module, and we can see in the corner list, and cohort view icons which are marked in the above image, and we can create multiple records for it. Click on the cohort view icon and you can see the report of the records day wise in the UI.