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

18 thoughts on “Creating and managing statusbars (selections) in Odoo

  1. Denis JOYE says:

    I trie your status bar application. Here is what I get when I click on “in progress” button :
    Traceback (most recent call last):
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 537, in _handle_exception
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 574, in dispatch
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 310, in _call_function
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\service\”, line 118, in wrapper
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 307, in checked_call
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 803, in __call__
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\.\openerp\”, line 403, in response_wrap
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\openerp\addons\web\controllers\”, line 948, in call_button
    File “C:\Program Files (x86)\Odoo 8.0-20151026\server\openerp\addons\web\controllers\”, line 936, in _call_kw
    AttributeError: ‘statusbar.demo’ object has no attribute ‘progress_progressbar’

  2. mahroza pradana says:

    Hi Yenthe666,

    i tried to follow your method , and in my case i can’t show the status bar colors. why yenthe? and i use statusbar_colors = ‘{“state” : “blue” }’. but my color nothing show. please tell me why to show the status bar color

    • Yenthe666 says:

      Hi mahroze,

      You’re mixing two concepts. The colors of a statusbar are defined with a class, the names of the items on a statusbar are defined with the states you’ve mentioned above.

  3. mohcine says:

    Thank you for this tutorial. I am a new user Odoo and I want to add lines of code when the status changes, I wonder if I have to go through workflows. thank you

    • Yenthe666 says:

      Hi mohcine,

      You can add an @api.depends on the field that is responsible for the state. You can then detect the state and do whatever you want.

  4. Hi Yenthe
    Thanks for this stuff as always, your material is cool!
    I just want to point that for further reference wuth users coming to this article nowadays like me, it would be good to remove the use of and use @api.multi / self.ensure_one instead huh?
    Thanks for your time!

    • Yenthe666 says:

      Hi Mateo,

      Thank you for the positive feedback! I should indeed replace that with an @api.multi. it is better not to use anymore. Will replace it as soon as I’ve got time. 🙂

Leave a Reply