Creating and managing statusbars (selections) in Odoo

Hi guys,

In this tutorial I will learn you how to create and manage statusbars. Statusbars are selections (on database level) which are visually respresented by Odoo. They give you the ability to follow a specific flow and can be very handy to show you the progress that has been made. After this tutorial you can create and manage your own statusbars.

In this example I will create a new form view which contains a statusbar and I’ll add buttons on the form which change the state of the record.
Updated tutorial available on Oocademy

1. Creating the model and fields

The first step is to create a model (if you don’t have one yet) and to add a selection field:

This creates a new model with the name ‘statusbar.demo’ which contains two fields, the field ‘name’ and the field ‘state’. The state field is of the type selection, which means it will contain multiple values which can be chosen. A selection is always built up in the same way, the first part if the database name and the second part is the text that will be shown to the user. For example:

If you wish to change the state to ‘In progress’ you will need to tell the database about ‘progress’, since this is the name the database listens to, while the name shown to the user will be ‘In progress’.
This is all you need on the database level. You have your selection values in the database so you only need to show them to the user now!

2. Creating the view

Now that you have the database part ready it is time to create your view. In this example I will create a simple form view which shows the field ‘name’ and the statusbar with some buttons:

So what does this tell us? Let us break this down part by part. The first thing you will notice is the field header.

Everything within these tags will be shown at the top of your Odoo form.
In this code example it will create four buttons and it will also show the field ‘state’. The field ‘state’ has a widget set to it that is made by Odoo and which will make the typical statusbar, just by adding a widget! This will generate the following bar at the top of your Odoo screen:
Statusbar and button example
Now there is one more thing I should explain, the buttons. By using those buttons we will modify in which state we are on the statusbar (selection) and we will control the whole flow. Let us take the button ‘In progress’ as an example to explain the code:

So what does this do exactly? It gives the button the text ‘In progress’ and we’ll give the button the type object, this will make it accessible in a function from the Python code later on. The name of the button will be the name of the function in your Python code and last but not least is the attrs. The attrs is a handy feature to make buttons visible/invisible when you’re in a state for example. In this example the button ‘In progress’ will not be shown when the statusbar is in the state ‘progress’.
Tip: If you have multiple states where buttons should be hidden you can use domains! For example:[(‘state’,’=’,[‘progress’,’finished’])]
Makes sense right? Let us continue and create the button functions!

3. Adding functions for the buttons

Now that the view is ready and you have buttons they should also trigger something. Open up your Python file and create a function for all of those button names you have. Every button will have a separate Python function and will modify the current record with a write. In this tutorial I will create four functions that simply change the state of what the current record is in:

When you now click on the button ‘In progress’ it will trigger the function ‘progress_progressbar’. The self.write will be triggered, which is telling Odoo that it should write on the current record, and it will modify the state to ‘finished’. Depending on in which state you are you’ll see buttons showing up and dissapearing. This is controller by the attrs value on the buttons. For example when I save my record and set it to in progress:
Statusbars in progress

4. 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.
Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy