Creating and using statusbars (selections)

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

Quick scroll

1. Introduction

Statusbars are often used in Odoo if the data you work with has to go through different states. It is a handy feature to show to a user in which state your record is and what still has to be done. In Odoo itself you can find this feature on sale orders, invoices, tasks and many more and it is a great visual way to show in which state your record is.

In this tutorial we will create a todo app with a statusbar on every todo record. We'll add buttons on the form view so that the user can set a todo to another phase. After this tutorial you can create your own statusbars and you'll know which configuration options there are.

2. Creating the model and fields

The first thing that we need is a model with a field that represents our statusbar. Statusbars are usually built from selection fields but you can also create statusbars from a many2one field if you'd like to have configurable states. Let us create a new model named 'to.do' and let's add a selection field that will become our statusbar later on. Create a new file named 'todo.py' in the models folder of your custom module and add in this code:

                    # -*- coding: utf-8 -*-from odoo import models, fields, apiclass ToDo(models.Model):    _name = 'to.do'    _rec_name = 'title'    _description = 'Todo'    title = fields.Char(string='Title', required=True)    description = fields.Html(string='Description')    progress_state = fields.Selection(        [            ('todo', 'To do'),            ('progress', 'In progress'),            ('done', 'Done')        ],        string='State',        default='todo'    )                                    

As you can see we'll create a new field named 'progress_state' which is a selection field. This selection field has three options: to do, in progress and done and by default this field will be set to todo. The values in this selection represent the states where a todo can go through and ofcourse we'd like it to be a record in the 'to do' state by default.

3. Creating the views

3.1 Creating the tree view

The next step is to create a view to show our records and to add our statusbar to it. Let's create a tree view so we can show all records and a form view for the details of one record. Let us start with a basic tree view that shows the title and the progress of the todo:

                                                                                

3.2 Creating the form view

Now let's create the form view with our statusbar. A statusbar always has to be within the header of the form so don't forget to add a header. Add the title and description on the form too. Have a look at this code first:

                                                                                

Let me explain this a bit more. Because the 'progress_state' field is within the header Odoo knows where to show the statusbar and how to outline it correctly. The widget="statusbar" tells Odoo that we want to show it as a statusbar. This means that every key in the selection will be used to build the bar. The clickable="True" option means that the user can change the state by clicking on it. The rest is common Odoo code. We basically show the other fields within a group on the form view.

3.3 statusbar options

There are some extra options you can set on a statusbar though:

  • clickable: if clickable is set to True or 1 the statusbar is clickable by the user. This means that the user can change the state directly. For example: clickable="1". If this option is not set the statusbar is by default not clickable and you'll have to handle states through the Python code.
  • statusbar_visible: defines when the statusbar is shown in Odoo. You can show the bar in specific states with this. For example: statusbar_visible="todo,done".
  • fold_field: if this option is set to fold this phase will be folded if you have a kanban view. For example: options={'fold_field': fold'}".
Finally you could also add a button on the form view and change the state of the statusbar through Python code. Simply create a button on the form that calls a Python function like this:
                                                                                                
In the Python code you can then do a write operation to change the state:
                        def set_done(self):        self.write({            # We update the state of the statusbar (selection) field by setting the key value.            'progress_state': 'done'        })                                    

4. 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_todo,to_do,model_to_do,,1,1,1,1                                    

5. 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 menuitem and an action to load all todo's.

5.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:

                                                                                

5.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 tree 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 form view with statusbar: Result statusbar

6. Conclusion

Statusbars (selections) are very handy in Odoo and give you the ability to show the progress to the user in a visual way. Statusbars are ideal to use in combination with buttons which will modify the state where a record is in. Another great thing about statusbars is that they're always at the exact same location and you can find out what the progress is within seconds.