Field Attributes
Attribute
Attributes are the properties. Every object has some attributes. In Odoo, every models,
fields, etc, have some properties that are the attributes. Here we have discussed some
model attributes.
The common attributes for a model are String, Required, Help, Index, Default, Readonly,
etc.
String
String is a common attribute used for every fields.
name = fields.Char(string="Record name")
Name is the variable name ‘char’ is the type of that field, and string is the attribute.
The string values are always added on double quotes (” ''). That value will show on the
UI.
Required
Is also another attribute for a field. At default, required is false. We can add that
required=True that will be working, and it is a mandatory field
name = fields.Char(string="Record name", required=True)
The required has only two values, either True or False.On the UI, the required fields are
make highlighted.
Here we can see the name field is highlighted. It is the mandatory field on this form
view.
Help
Provides long-form help tooltips for users in the UI.
name = fields.Char(string="Name", help="Enter the name")
Index
Requests that Odoo create a Database Index on the column.
name = fields.Char(string="Record name", index=True)
The index has two values True or False; by default the value is False.
Default
We can assign default values for the fields, so, we can use the attribute default for
that.
name = fields.Char(string="Record name", default='John')
The default value of the name field is the value of the default
Readonly
We can make a field as a readonly field means that we cannot change the field value
manually
name = fields.Char(string="Name", readonly=True)
Readonly have two values True and False readonly=True means that the field value cannot
be changed otherwise we can change the field value manually.
Translate
When set to true make the field translatable. It can hold different values, depending on
the user interface language.
name = fields.Char(string="Name", translate=True)
Groups
Make the field available only for some user groups
name = fields.Char(string="Name", groups=”base.user_group”)
States
Allows the user interface to set the value for the readonly, necessary, and invisible
attributes dynamically based on the state field's value.