Creating activity views in Odoo

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

Quick scroll

1. Introduction

The activity view is used to show an overview of all activities on a model. The activities will be shown in a table like view where every row is a record and column is the activity type of the activity. From this view you can directly edit and confirm tasks. In this tutorial you will learn how to create an activity view. We will create a new model, menuitem and view to show you how the activity view works.

2. Adding the dependency

Before you can start creating activity views you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add 'mail' as a dependency. The activity view inherits from the model 'mail.activity.mixin' which is defined in the 'mail' module so we'll need it:

                    'depends': ['mail'],                                    

3. Creating the model

The first thing that we need is a model. Let's create a new model named 'study.roadmap' and add a name field for the record names:
                    # -*- coding: utf-8 -*-from odoo import models, fields, apiclass StudyRoadmap(models.Model):    _name = 'study.roadmap'    # This inherit allows us to add activities on records (and thus to fill our activity views)    # It is because of this inherits that we'll need the mail module as a dependency.    _inherit = ['mail.thread', 'mail.activity.mixin']    _description = 'Study roadmap items'    name = fields.Char(string='Title', required=True)                                    

4. Creating the views

4.1 Creating the activity view

The activity view is one of the easiest views to create in XML. It just needs an activity string with a template inside it:

                                                                                

The field that is added inside the div 'activity-box' will be shown to the left of the activity view (as the title). You can even add multiple fields in the div and show them under eachother.

4.2 Creating the tree and form view

Lets add a tree and form view so we can access and edit the records made on this model too. Start with a tree view:

                                                                                

Lets also add a form view so we can edit the records:

                                                                                

Do you notice something interesting in the form view? In the form view definition I've added the 'message_ids' and 'activity_ids' fields. These fields exist in the mail module and contain all the data to be loaded in the chatter. 'message_ids' will hold and show all messages, both the internal and normal messages. The field 'activity_ids' will hold all activities related to the record. When you add these fields to the view you'll see them in the chatter just like you see on most existing views in Odoo.

5. Creating a security rule

Now that we have the views we'll need a security rule in the 'ir.model.access.csv' file 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_study_roadmap,study.roadmap,model_study_roadmap,,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

The last step is to create an action. The action will make sure that if you click on the menuitem that it will open up our activity view:

                                                                                

That's it! If you now install this module you'll have a fully working activity view: Activity view example

7. Conclusion

Activity views are a great way to get a quick overview for all activities related to a model. Thanks to the powerfull framework of Odoo it just takes a few lines of code to add activities to your own model and view while opening up a huge range of possibilities. Activities are a must have if you have to plan a lot of things or want to keep track off multiple activities.