Odoo
At times, we need a better understanding of data changes over a specific time period. In Odoo we can make use of cohort view for this scenario. Cohort view gives the report of data changes that take place over a time frame. Note that the Cohort view is only available in Odoo enterprise edition.
In order to understand how to create a cohort view in Odoo, let's create a 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. Add the fields. Cohort views needs 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 the 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 the date_start and end date of the record is taken from the date_stop, interval parameter gives the interval, here it is ‘day'
Add 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 action to view the cohort view. Create a menu item for the working of action (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.
Fields
View objects expose a number of fields. They are optional unless specified otherwise.
name (mandatory) Char
It is only useful as mnemonic or description of the view when looking for one in a list of some sort.
model Char
The model is linked to the view, if applicable.
priority Integer
When a view is requested by (model, type), the view matching the model and type, with the lowest priority will be returned (it is the default view).
It also defines the order of views of the application during view inheritance.
groups_id Many2many -> odoo.addons.base.models.res_users.Groups
The groups allowed to use/access the current view.
If the view extends an existing view, the extension will only be applied for a given user if the user has access to the provided groups_id.
arch Text
The description of the view layout.
Attributes
Different types of views have a wide variety of attributes allowing customizations of the generic behaviors. Some main attributes will be explained here. They do not all have an impact on all view types.
The current context and the user's access rights may also impact the view abilities.
create
Disable/enable record creation on the view.
edit (form & list & gantt)
Disable/enable record edition on the view.
delete (form & list)
We have to disable or enable record deletion on the view through the Action dropdown.
duplicate (form & list)
We have to disable or enable record duplication on the view through the Action dropdown.
decoration-$ (list & gantt)
Define a conditional display of a record in the style of a row's text based on the corresponding record's attributes.
Values are Python expressions. For each record, the expression of that record is evaluated with the record's attributes as context values and, if true, the corresponding style is applied to the row. Other context values are the uid (the id of the current user) and current_date (the current date as a string of the form YYYY-MM-DD).
<tree decoration-info="state == 'draft'"
decoration-danger="state == 'help_needed'"
decoration-bf="state='busy'">
<TREE_VIEW_CONTENT>
</tree>
We are an Odoo development company that provides custom Odoo development services for your ERP. We design SaaS models for every business size, industry, system, platform, and use cases. Our development services include integration with WFM, SCM, CRM, HRM, and accounting software. Talk to our experts and avail our all in-purchase Custom Odoo ERP development now!