Extend existing selection with selection_add in Odoo 10

In this tutorial I will teach you how to extend an existing selection through code in Odoo 10. You will learn how to extend the selection (without overriding the existing keys) and only adding new keys with the “selection_add” parameter.
In this tutorial I will extend the existing selection for an employee where the gender is set. After this toturial you will be able to add new keys to a selection.

1. Adding the correct dependencies

Alright let us start right away! First of all you will need to add a dependency to the module from which you want to extend the selection. In this tutorial I will need the “hr” module and I’ll add in the “contacts” module too because you’ll want to see the contacts anyway. Open up your __manifest__.py and add in the dependencies you need:

2. Inheriting the model

Now create a new Python file that inherits the model on which you want to extend an existing selection. In my example I’ll need to inherit ‘hr.employee’ as the field ‘gender’ is on this model:
Gender field on hr.employee
Your code should now look like this:

3. Extending the selection

Finally (or should I say already?) you can extend the field and add the new options you’d like. Inheriting the existing field is as simple as calling it with the same name again. Then just use the “selection_add” key to insert new options. Your code should be looking like this:

So what does this code do? Rather then having to give all key values again you can simply add only the new values you’d like. Odoo will automatically add these values to the selection without modifying anything to the existing keys. As a result this is the cleanest way to extend selections and prevents you from reassigning values every time. Your final code should now look like this:

After you’ve installed this custom code you’ll see that the new value is added because of this code:
Result selection field

4. Conclusion

Extending selections in Odoo has become very easy due to the “selection_add” parameter. It allows you to extend selections without having to override existing keys and removes some complexity and margin for errors.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!


Odoo Experts

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.

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!

Odoo Experts