Creating cohort views in Odoo
Beginner
The Cohort view is used to view and understand how data changes over a period of time. This view type, for example, allows you to easily follow how many people keep using a service you sell or how many people stopped the service. By clicking on a cell (value) the Cohort view will open a new view where you will only see the records that match within this cell it's interval. By default you can group a Cohort view on day, week, month and year. In this tutorial you will learn how to create your own Cohort view and what options you have. We will create a custom model named 'cohort.example' for which we will create a Cohort view.
Before you can start creating Cohort views you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add 'web_cohort' as a dependency:
'depends': ['web_cohort'],
Without this dependency set Odoo cannot build the view as 'web_cohort' contains all the framework logic to build this view.
# -*- coding: utf-8 -*-from odoo import models, fields, apiclass CohortExample(models.Model): _name = 'cohort.example' _description = 'Example model showing how cohort view works' name = fields.Char(string='Name', required=True) date_closed = fields.Datetime(string='Date closed', help='This date will be used in the Cohort view to define in' ' which column the record should be shown.')
The Cohort view is one of the easiest views to create in XML. It just needs a name, model, arch and a cohort parameter to create the view:
So, what does this exactly do? Because we add the 'cohort' attribute in the view Odoo knows it needs to create a Cohort. The 'date_start' field will tell Odoo what the start date of the record is (in this example the create_date). The 'date_stop' field will tell the Cohort view when this record has been stopped. For example: if this would have been a subscription we would take the field 'date' which holds the end date of the subscription. This field will be used to measure the retention rate. The 'interval="day"' parameter will tell Odoo that the Cohort view should group all records by day.
Finally we have the mode="churn" which will handle and categorise data by the default logic from Odoo. There are two modes: "churn" and "retention". Churn mode will start at 0% and accumulate over time whereas retention will start at 100% and decrease over time.
Now that we have the views we should add a security rule in 'ir.model.access.csv' to be able to access the view and create records in it. Let's create a simple security rule that gives all users full rights on this model:
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlinkaccess_cohort_example,cohort.example,model_cohort_example,,1,1,1,1
Alright, we're almost done! Now that we have the model, the views and the security rule we just need a menuitem with an action in order to open the view(s). In this tutorial I'll create a new app on the homescreen with a configuration menuitem and an action as I do not know where you'd want to build these settings. You can just change this example.
First we'll need a main menu item, which will show up as an app on the Odoo home screen. Just create a new menuitem with a name and an icon:
The last step is to create an action and submenu. The action will make sure that if you click on the menuitem that it will open up our cohort view:
Finally, add a child menuitem that is clickable and that also links to the action:
That's it! If you now install this module you'll have a fully working Cohort view:
Creating Cohort views in Odoo is very easy to do. At this point the Cohort view requires the least code and configuration of all views in Odoo even though the view is very powerfull. If you're looking to measure retention or churn this view is the way to go.