In this article you will learn how to make a field visible and invisible on the base of some condition. Here I am trying to make a field visible and invisible in Odoo wizard when "All" is checked or unchecked.

Making Fields Visible and Invisible Conditionally in Odoo


To hide fields in Odoo form or tree view we are going to use "attrs" in our field definition. By using this we can visible and invisible fields (based on some condition) from form as well as tree view.


visible_invisible_fields_based_on_condition_odoo.py 



Code Description

Here we declare three fields the first one is "country_id" which shows the name of country in a drop down list, the second field is "all_country" this field is Boolean (Check Box) and the last field is "is_show_country" on the basis of this last field we make our field visible or invisible. 


We will use this field in xml file to visible or invisible "country_id" field.

In line no 5 we used "@api.onchange" decorator on 'all_country', this means that whenever this check box checked or unchecked below functionality (line no 6 to 10) will be called.


visible_invisible_fields_based_on_condition_odoo.xml



Code Description

In line no 1 we declare "is_show_country" field attribute as invisible because we don't want to show this field in our form view or in wizard. Here we used this field to only visible or invisible "country_id" (which is to be declared in line no 2, "attrs="{'invisible':[('is_show_country','=', False)]}""). Whenever the value of "is_show_country" will be false then "country_id" field will be visible or vice versa.