Odoo 16 Development Book

Cohort view

One of the views that support Odoo is the cohort view, which gives information for the next 15 days, weeks, months, and years. So that we can get a good analysis of data in a specific time frame, let us look at an example.

from odoo import models, fields
class ExampleOdoo(models.Model):
    _name="example.odoo"
    example_field = fields.Char(string="Example Field")
    example_field1 = fields.Many2one('res.users', 'User', required=True)
from odoo import models, fields
class ModelExample(models.Model):
    _name = 'model.example'
    patient_id = fields.Many2one('hospital.patient', string="C")
    name = fields.Char(string='Name')
    date = fields.Date(string="Date")
    attachment = fields.Binary(string="Attachment")
class HospitalPatients(models.Model):
    _name = 'hospital.patient'
    name = fields.Char(string='name')
    dob = fields.Date(string='date')

After adding models, we can set the action and menu for these models.

<record id="action_Example1" model="ir.actions.act_window">
                                <field name="name">Example1</field>
                                <field name="type">ir.actions.act_window</field>
                                <field name="res_model">model.example</field>
                                <field name="view_mode">tree,form,cohort</field>
                            </record>
                            <menuitem id="model_example_menu"
                                      name="Model Example"
                                      parent="example_menu"
                                      action="action_Example1"/>

Now we can set the cohort view for the model.

<record id="model_example_cohort" model="ir.ui.view">
                                        <field name="name">model.example</field>
                                        <field name="model">model.example</field>
                                        <field name="arch" type="xml">
                                            <cohort string="model" date_start="date" date_stop="date" interval="day" sample="1"/>
                                        </field>
                                      </record>

Don't forget to add the ‘web_cohort’ in the depends otherwise it will encounter an error

There are two types of mode in cohort view which are retention and churn. Churn starts from zero and ends at zero. Retention will start from 100 % and decrease to zero

The output of the above code will be

odoo Development
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