Is it possible to implement a conditional attribute on a field in Odoo?

5.5k views Asked by At

Does anyone know if it is possible to implement a conditional attribute on a field?

For example:

<field name="name"/>

And if condition is met:

<field name="name" string="Custom Name"/>

I tried this approach:

<field name="name" attrs="{'invisible': [('condition', '=', False)]}"/>
<field name="name" string="Custom Name" attrs="{'invisible': [('condition', '=', True)]}"/>

When condition changes, label also changes, but then the field stop working properly. I mean it only registers entered values for second field name. If I change condition to False, it does not show the value that was entered, like that would be new field, even though it is the same field, just one time it is with string attribute, other without it.

3

There are 3 answers

0
ChesuCR On

You can only show each field in the form view once, as you found out. You only can use the following conditional attributes within the attrs attribute:

<field name="name" attrs="{'invisible': [('condition', '=', False)]}"/>
<field name="name2" attrs="{'readonly': [('condition', '=', False)]}"/>
<field name="name3" attrs="{'required': [('condition', '=', False)]}"/> 
0
Purnendu Singh On

If it is all about to update the label of the field based on condition then you should try fields_view_get.

0
OmaL On

What you can do is add labels.

<div >
    <label for="name" attrs="{'invisible': [('condition','=',False)]}"/>
    <label for="name" string="Custom String" attrs="{'invisible': [('condition','=',True)]}"/>
</div>
<field name="name" nolabel="1" />

only problem is that it will not have a vertical separator between label and field value.