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.
5 Comments
How can I find the field to a specific group of users? Thank you
ReplyDeletei cannot understand your problem...plz more elaborate.
DeleteThanks
Hello, I want to hide a field to a specific group, something like using attrs="{'invisible': [('FIELD1', '!=', 'SOMETHING')]}", but with groups. For example:
DeleteThe problem is that I do not know which of the fields of the "res.groups" model is in "YOUR GROUP". I have created a Sales / Manager group, but when I write that in "groups", I get an error. Thank you.
Thanks.
DeleteCreate a computed boolean field and make that field true if the login user has in your desired group. And use that field in the "attrs" to visible invisible fields.
all_fac_resume is stored in the database. Is that correct? Is it a good idea to have a database hold data just so we can have a visible/invisible field? Is there a way to have this like functional fields store=False?
ReplyDeleteYes you can do this without store field in database.