As an OpenERP/Odoo Developer, you might come across such a situation where you would like to make one or some fields read-only or mandatory or hide ('invisible' in Odoo terminology) based on the values of other fields. The solution here to your situation is 'attrs'.
What is (Attrs) Attribute in Odoo?
The attrs is an element in Odoo which is responsible to alter the attributes of a field, not the value. The 'attrs' attribute can be used to dynamically change the attributes of view components based on the value of other fields. Buttons in Odoo use state attributes for dynamic visibility, they too can be used with attars. It can be used on fields, pages, groups, buttons and notebook tags. Remember, there is no RPC call ever made because this is a pure client-side attribute at the view level. The structure of ' attrs' is a dictionary carrying the key as the client attribute and value as a set of domain(s), the keys could be:
Types of attributes in odoo
- Readonly
- Invisible
- Required
General Structure / Syntax to Use Attrs :
attrs="{'attribute': Domain which is if true, the element will wear the new property 'attribute'}"
Syntax of attrs :
<field name="value_amount" attrs="{'readonly': [('value', '=', 'balance')], 'invisible': ['|', ('service', 'in', ['ODOO Support', 'ODOO Training']), ('company', '=', 'SerpentCS')]}"/>
As per Above Example "value_amount" is field name and we applied attrs for "readonly" and "required" attribute.
Lets we see the real example to understand each attribute which we can use with the "attrs".
We created one model student. student to elaborate it more with a real example in Odoo / OpenERP.
class StudentStudent(models.Model):
_name = "student.student"
_description = "Student Details"
name = fields.Char(string="Student Name")
school_id = fields.Many2one('school.school', string="School")
school_code = fields.Char(related="school_id.sc_code",
string="School Code")
enrollment_name = fields.Char(string="Enrollment Number")
state = fields.Selection([('new', 'New'), ('confirm', 'Confirmed'),
('cancel', 'Cancelled')], default="new", string="state")
school_phone_no = fields.Char(string="School Phone No.")
Student Form View
<!-- Student Form View-->
<record id='view_form_student' model='ir.ui.view'>
<field name="name">view.form.student</field>
<field name="model">student.student</field>
<field name="arch" type="xml">
<form>
<header>
<button name="set_to_conform" string="Confirm"
type="object" class="oe_highlight"
attrs="{'invisible':[('state', 'in', ['confirm', 'cancel'])]}"/>
<button name="set_to_new" string="Set to Draft" type="object"
class="oe_highlight" attrs="{'invisible':[('state', 'in', ['new'])]}"/>
<button name="set_to_cancel" string="Cancel"
type="object" class="oe_highlight"
attrs="{'invisible':[('state', 'in', ['cancel'])]}"/>
<field name="state" widget="statusbar" statusbar_visible="new,confirm,cancel"/>
</header>
<sheet>
<div class="oe_title oe_left">
<label for="enrollment_name" class="oe_edit_only" string="Student Number"/>
<h1>
<field name="enrollment_name" default_focus="1" readonly="1"
placeholder="Student Number" class="oe_inline"/>
</h1>
</div>
<notebook>
<page string="Education Details">
<group col="2" colspan="4">
<field name="name" required="1"/>
<field name="school_id" attrs="{'readonly': [('state', '=', 'confirm')]}"/>
<field name="school_code" attrs="{'invisible': [('school_id', '=', False)]}"/>
<field name="school_phone_no" attrs="{'required': [('school_id', '!=', False)]}"/>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
Readonly :
- In Odoo / OpenERP if we have to make any 'field' or information readonly OR non-editable we can use "readonly" attribute.
- 'readonly' attribute we can use directly without 'attrs' OR with 'attrs' as per our need.
Situation :
Here we need to make school information "readonly" if student information is in 'confirmed' state. So we added Attrs on school_id field from xml view.
<field name="school_id" attrs="{'readonly': [('state', '=', 'confirm')]}"/>
Invisible :
- In Odoo / OpenERP if we have to make any 'field' or information hide / invisible, we can use "invisible" attribute.
- 'invisible' attribute we can use directly without 'attrs' OR with 'attrs' as per our need.
Situation :
Here we need to hide / invisible 'school code' information, if 'School' information is not available or selected. So we added Attrs on 'school_code' field from xml view.
<field name="school_code" attrs="{'invisible': [('school_id', '=', False)]}"/>
We can see ‘school’ information is not available so, school code inforation is not
shows as per at trs ‘school code’ information is hide.
After select or fi llup the School information we can see the school code
information on Student form view as per applied at trs on School code field.
Required :
- In Odoo / OpenERP if we have to make any ‘fi eld’ or information mandator y / required, we can use “required” attribute.
- ‘required’ at tribute we can use directly without ‘at trs’ OR with ‘attrs’ as per our need.
Situation :
Here we need to make required ‘school phone No.’ information, if ‘School’
information is available or selected. So we added At trs on ‘school_phone_no’ fi eld
from xml view.
<field name="school_phone_no" attrs="{'required': [('school_id', '!=', False)]}"/>
We can see ‘School’ information is available so, ‘School Phone No.’ Information is shows mandatory / Required as per attrs added on ‘school Phone No.’ field.
If we not add ‘School phone No.’ information and dirctly click on save but ton,
system will popup message and guide to add the required or mandator y
infomation.
Multiple Conditions :
We can also add multiple conditions to apply ‘readonly’, ‘required’ and ‘invisible’
attributes using the Attrs.
Let's see how we can add multiple conditions.
<fi eld name="school_phone_no" attrs="{'required': [ ‘|’, ('school_id', '!=', False),
( ‘school_code’, ‘!=’, False)]}"/>
Here, I have added t wo conditions and mentioned the ‘|’ operator as a logical
‘OR’ Operator. As per the logical condition it will make ‘school_phone_no’
information required.
As per the above condition it will check ‘school_id’ and ‘school_code’ both
information is available Or any of one information is available on same Student
Form then ‘school_phone_no’ fi eld will become mandatory / required.
<field name="school_phone_no" attrs="{'required': [('school_id', '!=', False),
( ‘school_code’, ‘!=’, False)]}"/>
If we don’t use the Logical operator between the multiple condition then by
default it will apply the Logical ‘AND’ operator . It means multiple condition which
we added that should be available or its result should be True then
‘school_phone_no’ will become required.
Column Invisible Option With Attrs :
Attribute name is ‘column_invisible’, it is used for One2many fi elds when we need
to hide any column in Child Table using the Parent model Information.
<field name="product_qty" attrs="{'column_invisible' : [('parent.hide_qty', '=',
False)]}"/>
As per above example ‘hide_qty’ is checkbox in parent table / model and
‘product_qty’ column / field is in One2many fi eld Or in Child table / Model.
So, if ‘hide_qty' check box is enable in parent model, ‘ product_qty’ column / fi eld
information will hide from Child table / model.
Connect with us & Explore More about SerpentCS
Web | www.serpentcs.com |
Solutions | www.serpentcs.in |
Apps | https://bit.ly/SCS_AppsStore |
YouTube | https://bit.ly/SCS_YTChannel |
https://bit.ly/SCS_LinkedIN | |
contact@serpentcs.com |