Tuesday 10 October 2017

Extend Existing Selection in Odoo 10



You might have ever wondered how to add more keys to an existing selection field in Odoo, well the truth is its very easy and i will put you through that in this tutorial.

This tutorial is assumed you know how to develop a module and add the necessary dependencies in the __manifest__.py file.

So for the purpose of this tutorial, we will make changes on the product model in the sales module, and whatever is done here can be replicated in any instance, in any module, you are trying to add more selection keys.


Step 1: Get the model, where you want to add your selection key. 



This the product form and the model name is template.product , we are adding more keys to the Product Type field.

In the Product Type field, we have Consumable,  Service and Stockable Product, just as shown below...



The essence of this above illustration is to fully understand what we have on ground, so we can be able to add what we want. So i hope we are on the same page. Let's move to the code part.

Step 2: Coding out our Wish

Now that we are ready to code what we want.

Open your editor, create or edit your python file and add this.

The last part of the code, does that magic

Now, load your server and install the module or refresh your browser and the changes has been done, just as you can see below;


Conclusion

You can see the process was short and easy, just with selection_add, and this usually used in odoo 10, if you search through other versions of odoo, you will barely see them, but you can also use the selection_add in version 9.

If you find this tutorial helpful, kindly comment below and share.

5 comments:

  1. I have pre-existing keys in a selection in a contact form. I added new keys using "selection_add" parameter and I wanted to find out what would be the opposite of a selection_add parameter to remove any old keys from a selection.

    ReplyDelete
    Replies
    1. Hi Jeff,

      I am not sure "selection_add" as an opposite, but can just re-declare the field and you should get what you want.

      Regards

      Delete
  2. What of using a radio button instead,if you click the radio button, you are able to view the fields that are under it..would really appreciate, thanks in advance.

    ReplyDelete
    Replies
    1. I hope you understand that, the radio button is enabled by the widget="radio" in your xml, so if you what to hide a field and show a field based on the selected radio button, you should use attrs on the fields you want to make visible.

      attrs="{'invisible': [('selected_field', '=', 'selection_key')]}"

      Delete