Hello Odooers & Developers!
Mastering Field Attributes (attrs) in Odoo
Welcome to this Odoo technical blog, where I'll share insights on using attrs in Odoo effectively. Attributes (attrs) in Odoo are essential for dynamically controlling field behavior in views. Whether you want to make a field invisible, read-only, or required based on certain conditions, attrs provides a powerful way to achieve this.
In this blog, we'll cover:
- What matters are and how do they work in Odoo
- Three types of matters:
- Invisible – Hide fields dynamically
- Readonly – Restrict editing based on conditions
- Required – Make fields mandatory when needed
Each section will include detailed explanations, practical examples, and real-world use cases to help you implement these attributes in your Odoo modules.
Dynamic Field Control with attrs in Odoo
attrs is a powerful attribute that allows developers to control the visibility and behavior of fields in forms and views based on certain conditions. This means you can dynamically visible, invisible, readonly, or required fields based on the values of other fields.
- Common Attributes :-
- invisible: This attribute hides or shows the field when the condition is met.
- Example 1: <field name="card_number" invisible= " state != 'draft' "/>
- Explanation:
- The invisible attribute in Odoo makes a field hidden or visible based on a condition. If the condition is True, the field is hidden; otherwise, it is shown.
- This means "Hide the card_number field if the state is not 'draft'."
- Example 2: we can use attrs in button also:
<button string=“Confirm” type= “object” name=“move_on_confirm” class=“oe_highlight” invisible= " state == ‘confirm’ " />
- Explanation:
- The invisible attribute in Odoo works the same way for buttons as it does for fields. It controls whether a button is visible or hidden based on a condition. If the condition is true, the button is hidden; if false, the button is visible.
- the button will only be visible if the state is not 'confirm'
- required: This attribute makes the field mandatory based on the condition.
- Example: <field name="purchase_date" required= "state == 'draft' "/>
- Explanation:
- The required attribute in Odoo makes a field mandatory based on a condition. If the condition is True, the field must be filled; otherwise, it is optional.
- readonly: This attribute makes the field read-only based on the condition.
- Example: <field name="gift_price" readonly=”state != 'draft' "/>
- Explanation:
- The readonly attribute makes a field non-editable based on a condition. If the condition is True, the field becomes read-only; otherwise, it remains editable.
Conclusion :
- The attrs attribute in Odoo is a powerful tool for dynamically controlling form fields. It allows you to:
- Hide fields when they are not needed (using invisible).
- Make fields required based on user input (using required).
- Prevent unwanted edits (using readonly).,With attrs, you can create dynamic and user-friendly forms that automatically adjust based on user selections.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
if you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.
How to Use attrs in Odoo ?