Skip to Content

Use of Attrs in Odoo

October 4, 2020 by
Use of Attrs in Odoo
iWesabe

    In Odoo, theattrs 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': ['&amp;', ('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.


If you wish to have the latest updates from us, you may follow us on

in Odoo
Use of Attrs in Odoo
iWesabe October 4, 2020
Share this post

Great stories have a personality. Consider telling a great story that provides personality. Writing a story with personality for potential clients will assist with making a relationship connection. This shows up in small quirks like word choices or phrases. Write from your point of view, not from someone else's experience.

Great stories are for everyone even when only written for just one person. If you try to write with a wide, general audience in mind, your story will sound fake and lack emotion. No one will be interested. Write for one person. If it’s genuine for the one, it’s genuine for the rest.

Tags

Enhance Your Experience

Write one or two paragraphs describing your product or services. To be successful your content needs to be useful to your readers.

Start with the customer – find out what they want and give it to them.

Learn more

Archive

Enhance Your Experience

Write one or two paragraphs describing your product or services. To be successful your content needs to be useful to your readers.

Start with the customer – find out what they want and give it to them.

Learn more

Enhance Your Experience

Write one or two paragraphs describing your product or services. To be successful your content needs to be useful to your readers.

Start with the customer – find out what they want and give it to them.

Learn more