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!




PayPal

Odoo Experts