Development Book V17:Field Attributes

Attribute

Attributes are properties. Every object has some attributes. In Odoo, each model, field, etc., has some properties that are attributes. In this section, we will go over some model attributes.

A model's most common attributes are String, Required, Default, Readonly, Help, Index, and so on.

String

String is the common attribute for all fields.

name = fields.Char(string="Field name")

"name" refers to the variable name, "Char" refers to the field type, and string refers to the attribute. String values are always surrounded by single or double quotes. This value will be displayed on the UI.

Required

Required is set to false by default. We can add required=True to make them mandatory fields.

name = fields.Char(string="Field name", required=True)

True or False are the only valid values for required fields.

Help

In the UI, it displays help tooltips for users.

name = fields.Char(string="Name", help="Name of the applicant")
odoo-creating-odoo-modules

I've added the help attribute to the name field.

Index

Requests that Odoo creates a Database Index on the column.

name = fields.Char(string="Record name", index=True)

True or False are the only values that can be assigned to the index attribute. The value is False by default.

Default

Using the default attributes, we can set default values for the fields.

name = fields.Char(string="Field name", default='John')

The name field's default value will be the value of the default.

Readonly

If we mark a field as read-only, we cannot manually change its value.

name = fields.Char(string="Name", readonly=True)

Readonly has two values: True and False; readonly=True indicates that the field's value cannot be changed.

Translate

When the translate attribute is set to true, the field can be translated. It can have different values depending on the user interface language.

name = fields.Char(string="Name", translate=True)
Groups

It is used to restrict access to a field to specific user groups.

name = fields.Char(string="Name", groups="base.user_group")
WhatsApp