Creating cohort views in Odoo

< / / / / / >
Beginner
Views
V13.0
V 13.0
+/- 7 minutes
Written by Yenthe Van Ginneken
(0)

Quick scroll

1. Introduction

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.

2. Adding the dependency

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.

3. Creating the model

The first thing that we need is a model. Let's create a model named 'cohort.example' and let us create the minimum amount of fields we need. We'll want a title for the record and the Cohort view needs atleast one Datetime field in order to work:
                    # -*- 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.')                                    

4. Creating the view

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.

5. Creating a security rule

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                                    

6. Creating the menuitem and action to open it

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.

6.1 Creating a main menuitem

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:

                                                                                

6.2 Creating an action and submenu

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: Cohort view example

7. Conclusion

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.