DIfferent Attributes Types Used in Fields in Odoo 16 And Their Uses

TEAM-BASSAM
April 20, 2023

In Odoo, fields are an integral part of the form view or tree or any other view. Depending upon the client’s requirements, those fields were subjected to some changes. For example:

If a field needs to be a required field that means the value must be given to the particular field. So for that case, there is a feature called attributes, which we can add to the fields in order to change the behavior of fields.

Some of the important attributes are:

1. invisible

2. required

3. read-only

1. Invisible:

For making a newly added field invisible, we have to follow this code:

Assume the field account_id need to make invisible, so the code will be:

<field name=”account_id” attrs=”{‘invisible’:[(‘is_po’,’=’,True)]}”/>

Here we can see there is a field is_po. That field is a boolean field and is set to False. So the condition explains that whenever the boolean becomes true, the field becomes invisible. So attributes work on a condition.

If the field is in the server code, then we can inherit the particular view in which the field is declared, and the using XPath expression, we can make the field invisible like this:

<xpath expr=”//field[@name=’account_id’]” position=”attributes”>

<attribute name=”attrs”>{‘invisible’:[(‘is_po’,’=’,True)]}</attribute>

</xpath>

In this way, invisible attributes work.

2. required

For making a newly added field required, we have to follow this code:

Assume the field account_id need to make required, so the code will be:

<field name=”account_id” attrs=”{‘required’:[(‘is_po’,’=’,True)]}”/>

Here the condition explains that whenever the boolean field becomes true, the field account_id becomes invisible.

If the particular field is in the server code, then like for the invisible, same way we can xpath like:

<xpath expr=”//field[@name=’account_id’]” position=”attributes”>

<attribute name=”attrs”>{‘required’:[(‘is_po’,’=’,True)]}</attribute>

</xpath>

3. read-only

 The read-only attribute makes a field read-only which means a particular field cannot be edited by the user. So for making a newly added field read-only, we have to follow this code:

<field name=”account_id”  attrs=”{‘read-only:[(‘is_po’,’=’,True)]}”/>

Here the condition explains that whenever the boolean field becomes true, the field account_id becomes read-only.

If the particular field is in the server code, then the code will be as follows:

<xpath expr=”//field[@name=’account_id’]” position=”attributes”>

<attribute name=”attrs”>{‘read-only:[(‘is_po’,’=’,True)]}</attribute>

</xpath>

So in this way, the attributes are added in fields in XML files.

If there is no condition and if the field needs to  be always read-only, then we can define it as:

<field name=”account_id”  readonly=True/>

In the py file also, we can give the attributes.

So for required fields, the code would be like this:

Account_id = fields.Many2one(“account.account’,string=”Account”,required=”1”)

We can either give 1 or True in the conditions. Both are the same.

For read-only also, the same way we can declare the field.

Account_id = fields.Many2one(“account.account’,string=”Account”,readonly=”1”)

So there are methods for defining attributes for the fields in Odoo.

Request Your Free Quote

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *