Thursday, 6 October 2016

How to visible and invisible fields in odoo

Today you will learn how to visible and invisible Odoo fields. Here I am trying to make a field visible and invisible in Odoo wizard when "All" is checked or unchecked.

In the above screen shot when the "All" check box is checked, the "Faculty Name" field should be invisible and vice versa.

faculty_id = fields.Many2one('model.faculty', string="Faculty Name")
all_fac_resume = fields.Boolean(string="All")
is_show_faculty = fields.Boolean(string="is show faculty")

1    @api.onchange('all_fac_resume')
2    def _all_checked(self):
3        if self.all_fac_resume:
4            self.is_show_faculty = False
5        else:           
6            self.is_show_faculty = True

Code Description:
Here we declare three fields the first one is "faculty_id" which shows the name of faculty in a drop down list, the second is "all_fac_resume" this field is Boolean (Check Box) and the last field is "is_show_faculty" on the basis of this last field we make our visible or invisible functionality. Here we will use this field in our xml file to visible or invisible "faculty_id" field.

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


1<field name="is_show_faculty" attrs="{'invisible':1}"/>
2<field name="faculty_id" options="{'no_quick_create': True,'no_create_edit':True}" style="width: 300px" attrs="{'invisible':[('is_show_faculty','=', False)]}"/>

Code Description:
In line no 1 we declare "is_show_faculty" 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 "faculty_id" (which is to be declared in line no 2, "attrs="{'invisible':[('is_show_faculty','=', False)]}""). Whenever the value of "is_show_faculty" will be false then "faculty_id" field will be invisible or vice versa.



  1. How can I find the field to a specific group of users? Thank you

    1. i cannot understand your problem...plz more elaborate.

    2. Hello, I want to hide a field to a specific group, something like using attrs="{'invisible': [('FIELD1', '!=', 'SOMETHING')]}", but with groups. For example:

      The 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.

    3. Thanks.
      Create 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.

  2. 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?

    Yes you can do this without store field in database.