Now let's look at how to create a cohort view in Odoo. Let’s create a new module to test how to create a cohort view.
Here I am creating a module cohort_test. While configuring the manifest.py file gives dependency to ‘web_cohort’, web_cohort has the framework to create the cohort view.
Add this in manifest.py file:
'depends': ['web_cohort'],
Create the model. I have created a model ‘cohort.cohort’ for testing. And add the fields. Cohort views need at least one date field in order to work.
class CohortTest(models.Model):
_name = 'cohort.cohort'
name = fields.Char(string='Name', required=True)
date_end = fields.Datetime(string='Date end')
Now create the view in XML,
<record id="view_cohort_cohort" model="ir.ui.view">
<field name="name">cohort.cohort.test</field>
<field name="model">cohort.cohort</field>
<field name="arch" type="xml">
<cohort string="Cohort records" date_start="create_date" date_stop="date_end" interval="day" mode="churn"/>
</field>
</record>
Add the ‘cohort’ attribute in view so Odoo can create the cohort view. There are two modes: "churn" and "retention". Churn mode will start at 0% and increase over time whereas retention will start at 100% and decrease over time. Here mode="churn" is used. Start date of the record is taken from date_start and end date of the record is taken from date_stop, interval parameter will give the interval, here it is ‘day’
Add a security rule in 'ir.model.access.csv' to access the view.
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,per_unlink
Access_cohort_cohort,cohort.cohort,model_cohort_cohort,base.group_user,1,1,1,1
Now create the menu item.
<menuitem id="cohort_menu_root" name="Cohort Test"/>
This will show as an App.
Now create action and submenu.
<record id="action_cohort" model="ir.actions.act_window">
<field name="name">Records</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">cohort.cohort</field>
<field name="view_mode">tree,form,cohort</field>
</record>
This XML code will give the action to view the cohort view. Create a menu item for the working of action ie the submenu,
<menuitem id="cohort_menu" name="Records" action="action_cohort" parent="cohort_menu_root" sequence="1"/>
This menu item will open the cohort view.
Now install the module. We will get the cohort view like the below image.