Odoo: Conditional invisible attribute on fields only works in one direction?

RobbeM picture RobbeM · Aug 28, 2015 · Viewed 19.9k times · Source

I'm trying to make a field invisible on condition in an Odoo form view. When "Can be sold" is checked ==> "Product Manager" should be invisible:

enter image description here

enter image description here

I tried using the attribute "invisible" with a domain in the inherited view of the products form:

<record model="ir.ui.view" id="product_template_form_inherit">
    <field name="name">product.template.product.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_only_form_view" />
    <field name="arch" type="xml">
        <field name="product_manager"  position="attributes">
                    <attribute name="invisible">[('sale_ok', '=', True)]</attribute>
        </field>    
</field>
</record>

When the field sale_ok is true, the product_manager field is actually hidden. But when field sale_ok becomes false again, the field product_manager stays hidden.

I also tried this instead:

<field name="product_manager" attrs="{'invisible': [('sale_ok', '=', True)]}"/>

This doesn't work either.

I've also tried other domains like:

[('sale_ok', '==', True)]
[('sale_ok', '!=', False)]
[('sale_ok', '=', 'True')]

Not really sure what's wrong here... How to make it (in)visible when (un)checked?

What I'm eventually after is the following thing: When a checkbox is checked, the form should change immediately without saving. Fields have to be added and removed. Is that possible?

Edit:

I can hide/unhide product manager now with ChesuCR's answer. However when I try the same thing with "loc_rack" (Storage Location==>Rack) it gives error:

Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Element '<field name="loc_rack">' cannot be located in parent view

This is the code I used:

<field name="loc_rack"  position="replace">
    <field name="loc_rack" attrs="{'invisible': [('sale_ok', '=', True)]}"/>
</field>

Why can't I do the same with this field?

Answer

ChesuCR picture ChesuCR · Aug 28, 2015

This works well to me

<record id="custom_product_template_form_view" model="ir.ui.view">
    <field name="name">custom.product.template.form</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view" />
    <field name="arch" type="xml">
        <field name="product_manager"  position="replace">
            <field name="product_manager" attrs="{'invisible': [('sale_ok', '=', True)]}"/>
        </field>
    </field>  
</record>

If you find any problems you can try the "federico" answer just to modify the attrs attribute. My solution may modify or remove other attributes if they already exist in the original form.