Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Conditionally display or hide a field in a view

on 4/23/13, 2:53 PM 25,165 views

Hi everyone, I asked please about a question in the file.xml how i can display a view depending on a field. for example,i have a module with a field married or not.i want to have a special view following the field



Francesco OpenCode

--Francesco OpenCode--
| 5 7 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 4/23/13, 3:06 PM

In OpenERP is possibile to show or hide field in relation to another field (or fields) value. For example, if you have married field and boyfriend_name as field to show if married is true, you can use this system:

<field name="boyfriend_name" attrs="{'invisible':[('married', '!=', False)]}" />

You're telling to OpenERP to hide ({'invisible':...) the field boyfriend_name if married is not false (it's true!!)

Another example is the comparation with another type of field. For example a selection.

<field name="boyfriend_name" attrs="{'invisible':[('married', '!=', 'selection_key')]}" />

With this system you can create your view dinamically based on your field value

Hello, could you tell me how can i do that if the married field is in on another model?

voathnak lim
on 5/30/13, 5:22 AM

If the field is in another class, you must insert a related field in the current class and set the attrs on this one.

Francesco OpenCode
on 5/30/13, 7:33 AM

It works great.

on 2/19/14, 9:39 AM
Dharmesh Rathod
On 4/24/13, 1:53 AM


You are using following which is correct for selection field:

attrs="{'invisible':[('lang_mat', '!=', 'eng')]}"

You can use another operators like : 'in' or 'not in' and check.

Email : info@acespritech.com
Skype : acespritech

Thank you for your answer it works now...I had an error on the bug windows :)

on 4/24/13, 7:25 AM

Andreas Maertens

--Andreas Maertens--
| 5 5 7
Finsterwalde, Germany
--Andreas Maertens--

I develop odoo8.0 modules

Andreas Maertens
On 4/24/13, 7:51 AM

You can add an invisible field on your view (invisible="1") relating to a function field in your model. There you can evolve by anything you want and return a stringm bool or number and so in to evaluate if another field is visible or maybe readonly:

The View:

<field name="write_if_manager" attrs="{'readonly':['|',('is_manager', '==', 'false'),('state','not in','['draft']') ] }/>
<field name="is_manager" invisible="1"/>

The class looks like

class classname(osv.osv): 
    def _is_manager(cr, uid, ids ...
        res = {}
        for this_id in ids:
            if obj_users.has_group(cr, uid, 'MANAGER-GROUPNAME'):
                res[this_id] = manager
        return res
    _columns = {
        'is_manager':fields.function(_is_manager, type='boolean' ...
        'write_if_manager':fields.char( ...)

Nice Its working good..

Ankit H Gandhi(AHG)
on 8/27/15, 6:23 AM
On 7/30/16, 12:14 AM

I have an issue please help me.

<record id="view_order_form_inherit" model="ir.ui.view">
 <field name="name">sale.order.form</field>
 <field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
 <field name="arch" type="xml">
<xpath expr="//field[@name='payment_term_id']" position="after"> 
   <field name="head_option"/>
    <xpath expr="//field[@name='order_line']/tree/field[@name='product_id']" position="before">
         <field name="head" attrs="{'required':[('head_option','=',True)], 'invisible':[('head_option','=',False)]}"/>

he head option is a Boolean field that is in the sale order. and head option is in the sale order_line tree view..

so attrs can't work.
replay ur answers,...

on 7/30/16, 1:39 AM
On 4/23/13, 8:03 PM

Hi,thank u for your answer;it work's with a boolean fields but i have an error when the type of field is a selection it doesn't work...


class student(osv.osv): 
    _name = 'student'
    _order = 'name, prenom'
    _description = 'Informations sur l etudiants'
    _columns = {
        'name': fields.char('Nom Famille', size=64),
        'prenom': fields.char('Prenom', size=64),
        'lang_mat':fields.selection([('eng','English'), ('fr','French')], 'Langue Maternelle'),



<openerp> <data> <menuitem name="marriedbis" id="menu_stage_parent"/>
<menuitem name="enseignant" id="menu_enseignant_parent1" parent="menu_stage_parent"/>

<record model="ir.ui.view" id="view_school_student_form">
    <field name="name">student.form</field>
    <field name="model">student</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Information Eleve">
                    <field name="name" colspan="6" />
                    <field name="prenom" colspan="6"/>  
                    <field name="married" colspan="6"/> 
                    <field name="lang_mat" colspan="6"/>    
                    <field name="boy" attrs="{'invisible':[('lang_mat', '!=', 'eng')]}" />

<record model="ir.ui.view" id="view_school_student_tree">
    <field name="name">student.tree</field>
    <field name="model">student</field>
    <field name="type">tree</field>
    <field name="arch" type="xml">
        <tree string="stages.enseignant">
            <field name="name"/>
            <field name="prenom"/>
<record model="ir.actions.act_window" id="action_school_student_form">
    <field name="name">Eleves</field>
    <field name="res_model">student</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>

<!--  Et finalement il y a le menu qui contient l'action pour ouvrir la vue tree de ton module -->

<menuitem action="action_school_student_form" id="menu_stages_enseignant" parent="menu_enseignant_parent1" />

</data> </openerp>

The attrs values works witrh all the type of field. If you nedd to hide a field in relation a selection you must compare che field to show/hide with the selection field. I've updated my answer.

Francesco OpenCode
on 4/24/13, 5:15 AM

Thank you for you're answer;it works now :)

on 4/24/13, 7:22 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)


Asked: 4/23/13, 2:53 PM
Seen: 25165 times
Last updated: 7/30/16, 12:14 AM