In Odoo, each field of a model plays a crucial role in the definition and functioning of modules. These fields are endowed with various attributes that determine their behavior, accessibility, and interaction with the database and the user interface. Whether you are an experienced developer or new to using Odoo, understanding these attributes is essential for creating effective and customized applications. In this article, we will explore the different attributes of Odoo fields and their importance in module development. This will help you optimize your models and make the most of the Odoo platform.
Attribute Descriptions
- String: The readable label of the field, used in views or reports.
name = fields.Char(string="Name")
- Required: Makes a field mandatory. The system will not allow saving without a value for this field.
name = fields.Char(string="Name", required=True)
- Readonly: Makes the field unmodifiable in the user interface, useful for calculated or reference data.
total = fields.Float(string="Total", readonly=True)
- OnDelete: Defines the behavior when a record referenced by a Many2one field is deleted.
partner_id = fields.Many2one('res.partner', ondelete='cascade')
- Copy: Controls whether the field's value should be copied when duplicating the record.
description = fields.Text(copy=False)
- Tracking: Enables tracking of changes for auditing purposes.
status = fields.Selection(tracking=True)
- Help: Provides tooltip text for the field in the form view.
age = fields.Integer(help="Enter your age.")
- Translate: Allows translation of the field's value, essential for multilingual support.
description = fields.Text(translate=True)
- Search: Determines whether the field can be searched in the search view.
# Custom search function
def _search_function(self, operator, value):
# search logic
return [('id', 'in', ids)]
name = fields.Char(search='_search_function')
- Groups: Restricts the field's visibility to certain user groups.
confidential_info = fields.Char(groups="base.group_system")
- Index: If True, an index is created for this field in the database.
email = fields.Char(index=True)
- Default: Sets a default value for the field when creating new records.
age = fields.Integer(default=30)
- Compute: Links the field to a computed method.
total = fields.Float(compute='_compute_total')
@api.depends('value1', 'value2')
def _compute_total(self):
for record in self:
record.total = record.value1 + record.value2
- Store: Determines whether the field's value is stored in the database.
total = fields.Float(compute='_compute_total', store=True)
- Delegate: Used in inheritance to make the fields of the parent model accessible as if they were fields of the child model.
# In a child class
_inherits = {'parent.model.name': 'parent_id'}
parent_id = fields.Many2one('parent.model.name', ondelete='cascade', delegate=True)
- Inverse: For computed fields, specifies the method that sets the field's value.
name = fields.Char(compute='_compute_name', inverse='_inverse_name')
def _inverse_name(self):
for record in self:
# inverse logic
- Depends: For computed fields, lists the fields on which the calculation depends.
country_code = fields.Char(related='country_id.code', depends=['country_id'], readonly=True)
- Selection: Provides a list of possible values for selection-type fields.
state = fields.Selection(selection=[
('draft', 'Draft'),
('confirmed', 'Confirmed'),
('done', 'Done'),
], default='draft')
We hope this overview of Odoo field attributes and their code examples has been helpful. These elements are essential for fully leveraging Odoo's capabilities and for developing effective and personalized modules. Feel free to share your experiences, tips, or questions in the comments below. Your feedback enriches our community and helps everyone grow in the Odoo universe. If you have suggestions or specific points you would like to see addressed in future articles, let us know!
Understanding Field Attributes in Odoo: An Essential Guide