Odoo includes different types of fields designed to create relationships between different models and regulate data access. These fields play a key role in creating connections between models within Odoo. Automatic and reserved fields are two specific types that serve to achieve this goal.
In this blog, we'll dive into exploring Automatic and Reserved fields and clarify their importance in facilitating the creation of relationships between models.
Automatic fields in Odoo are automatically generated by the system and do not require an explicit definition in the model. For example, the 'id' field is automatically generated and provides a unique value for each record in the database. On the other hand, reserved fields are system-defined fields that cannot be used as custom field names. These fields are primarily reserved for system-generated information or metadata and serve specific functions within the system.
Now, let's explore each field in more detail.
id
Odoo's field. id is designed to indicate the database column responsible for storing a unique identifier (ID) for each record within the model. Each record has its own distinct ID, which is automatically set upon creation and remains unchangeable thereafter. The Identifier field proves invaluable for accurately identifying individual records. Debug mode must be enabled to access and view this field. Open debug mode and go to “Show fields.
create_date
This is a system-defined field within Odoo designed to store the date and time a record was created in the database. As an automatic field, it is generated and managed by the system and remains immutable by the user.
write_date
This field represents the date and time the record was last updated. As a record undergoes editing, this field is automatically updated to capture the appropriate date and time. It can be used to filter records based on their last update timestamp or to display the last update date and time in a record form or report. This field is stored as a read-only attribute in each record.
create_uid
This is a many2one field associated with the comodel 'res.users,' capturing the user_id of the record creator. Automatically populated upon the creation of a new record, this system field facilitates tracking the user responsible for a specific record, aiding in auditing and debugging. It also supports access control enforcement, restricting record creation to specific users. This field is designated as read-only.
write_uid
This is a many2one field associated with the 'res.users' comodel that stores the user_id of the last record modifier. This system field, known as write_uid, is automatically updated whenever a record is modified and is used to track the user responsible for the latest changes. It has proven valuable for auditing and debugging purposes and can be used to enforce access control, restricting record updates to specific users. Additionally, it represents the date and time the record was last modified.
Access Log Fields
These fields are automatically generated and updated when log_access is set to true. They are utilized for monitoring changes to records and identifying the users responsible for those modifications.
Disabling log_access prevents the unnecessary creation or update of these fields in tables where they are not required. Typically, log_access is set to true in transient models, which store data temporarily and are subject to periodic deletion.
name
This field is a required attribute on every character type model. It is used to display the title of each record and is commonly used to store labels for objects or entities in the system such as customers, products or employees.
For instance:
from odoo import models, fields
class MyModel(models.Model):
_name = 'my.model'
_description = 'Model'
name = fields.Char(string='Name', required=True)
In the above example, the 'name' field is defined in the 'my.model' class, playing a crucial role in identifying and differentiating records within the model. Typically, this field is presented as the primary label for records in various views and forms, including tree views, search views, and form views.
active
This field controls the visibility of the view. The Odoo.fields.active refers to a Boolean field utilized for marking records as active or inactive. When a record is marked as inactive, it effectively becomes "hidden" in most views and reports. However, the recording data continues to exist in the database and can be retrieved and reactivated later if needed. This feature is commonly employed as a means to temporarily disable records without permanent deletion.
Example:
active = fields.Boolean('Active', default=True)
In this example, when the active field is set to True, the records will be visible in views and reports.
State
This field type indicates a selection field used to indicate a state or condition for an item in the model. It is commonly used to track the progress or workflow of a record as it goes through various stages such as "draft", "submit", "cancel" and so on. Each state value is defined as a tuple that contains a string label and a unique identifier.
Example:
state = fields.Selection(
selection=[
('draft', 'Draft'),
('posted', 'Posted'),
('cancel', 'Cancelled'),
],
string='Status',
required=True,
readonly=True,
copy=False,
tracking=True,
default='draft',
)
In this example, the 'state' field helps articulate various actions or behaviors triggered when a record transitions from one state to another.
company_id
Odoo’s field company_id facilitates multi-company functionality, representing a many-to-one reference field used to associate a record in a model with a specific company. This box allows multiple companies to use the same Odoo instance while keeping their data and configurations separate.
Example:
company_id = fields.Many2one('res.company', string="Company", help='company',
default=lambda self: self.env.user.company_id)
company_id ensures accurate assignment of transactions and documents to the respective company. It is of type 'many2one' and refers to the 'res.company' model. The given example shows the use of automatic and reserved fields in Odoo 17.