Published 2nd May 2022
In Odoo, the attrs is helps to dynamically change the attributes of view components based on the value of other fields. Sometime we might have come across a situation where we need to conditionally apply attributes to the fields. It is used on field, page, group, button, notebook and div. The attrs element is used to hide a field (invisible), make the field mandatory (required), or read-only based on another field’s value. Following are some examples of attrs uses in Odoo.
The syntax for hiding (invisible) a field using attrs:
<fieldname="type_id" attrs="{'invisible': [('state','!=','draft')]}"/>Here in this code the attrs as invisible and when the state not equal to draft it makes the field as invisible.
We can also add multiple conditions in attrs.
<field name="type_id" attrs="{'invisible': [('invoice_group', '=', False), ('state','!=','paid')]}"/>In the above example, invisible becomes true only if both the conditions are true. In the following example, a logical OR operator is used in the condition. So if any of the conditions are becomes true then the field will return invisible.
<field name="type_id" attrs="{'invisible': ['|', ('invoice_group', '=', False), ('state','!=','paid')]}"/>
Example of three conditions:
<fieldname="type_id" attrs="{'invisible': ['|', '|', ('type', 'not in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund')), ('state', '!=', 'posted'), ('authorized_transaction_ids', '=', [])]}"/>
Attrs in Page:
<page string="Optional Products" name="optional_products" attrs="{'invisible': [('state', 'not in', ['draft', 'sent'])]}">
<!........your code........!>
</page>
Attrs in Group:
<group attrs="{'invisible': [('display_type', '!=', False)]}">
<!........your code........!>
</group>
Attrs in Button:
<button name="button_add_to_order" class="oe_link" icon="fa-shopping-cart" title="Add to order lines" type="object" attrs="{'invisible': [('is_present', '=', True)]}"/>
Attrs in Notebook:
<notebook attrs="{'invisible': ['&', ('module_id', '!=', False), ('module_state', '!=', 'installed')]}">
<!........your code........!>
</notebook>
Attrs in Div:
<div attrs="{'invisible': [('module_account', '=', False)]}">
<!........your code........!>
</div>
The syntax for making afield required using attrs:
<fieldname="type_id" attrs="{'required': [('state','!=','draft')]}"/>Here in this code the attrs as required and when the state not equal to draft it makes the field as required.
The syntax for making a field read-only using attrs:
<fieldname="type_id" attrs="{'readonly': [('state','!=','draft')]}"/>Here in this code the attrs as read-only and when the state not equal to draft it makes the field as read-only.
Published 2nd May 2022
Please fill all the details and give your valuable comments