Enable Dark Mode!
what-are-automatic-and-reserved-fields-in-odoo-18.jpg
By: Cybrosys Technologies

What are Automatic & Reserved Fields in Odoo 18

Technical

As an open-source platform, Odoo’s importance lies in the flexibility, scalability, and affordability of usage and development. While Odoo itself is written primarily in Python and JavaScript, its value as an open-source ERP system transcends specific programming languages, offering businesses across industries a customizable and highly adaptable solution for managing their operations.
While talking about the development aspects of Odoo 18 modules, the term fields are the building blocks of models, representing the data stored in the system. These fields define the structure of data in Odoo models (tables in the database) and are used to create forms, views, and manage business logic. Fields actually refer to the attributes or properties of a mapped class that correspond to columns in a database table.
In Odoo 18, the terms Automatic fields and Reserved fields refers to the fields that the system manages internally for various functionalities. These fields are generally not explicitly created by developers or users but are either automatically added by Odoo based on the model definitions or are predefined fields that Odoo reserves for specific purposes.
Let’s explore both of them in detail with examples.

1. Automatic fields

The Automatic fields are the ones that Odoo automatically manages or generates based on the requirements and configurations needed for the models that are being created. These fields are usually added by the Object Relational Mapping(ORM), which is the bridge between the programming language and database.

* id

This automatic field is the one responsible for the unique identification of every record in the model. Each record must contain its own distinct ID, which is managed automatically by the system and remains unchanged forever. After enabling the instance in debug mode, the ‘id’ field can be seen on the URL of the record.

* create_date

This field contains the timestamp when the record was created and is stored in this field. The system maintains the complete responsibility of this field. Also, the user cannot change it since it is added automatically

* create_uid

The user who last created the record will get saved in this automatic field. This is actually a Many2one field with the co-model set as ‘res.users’. So, create_uid contains the id of the creator who is responsible for the record.

* write_date

This field contains the timestamp when the record was last modified. ‘write_date’ is also a read-only attribute for every record as the system automatically updates the value at the moment when the record is saved with a change in any other field’s value.

* write_uid

This system field, known as write_uid is also a May2one field with ‘res.users’ as the co-model, which saves the Id of the user who last modified the record. This automatic field is actually used for the tracking of the latest update done on the record and it keeps the details, such as 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.
These fields are automatically added to every model, allowing the system to track who created and modified each record, as well as when those changes were made.

* log_access

In Odoo, the log_access field is a special attribute used at the model level to enable or disable the automatic logging of access metadata, such as the creation and modification timestamps, as well as the user who performed these actions. By default, Odoo logs this data for all models, but you can disable it by setting log_access = False
If you create a model without disabling log_access, Odoo will automatically include the fields create_uid, create_date, write_uid, and write_date in the database. That is, it allows developers to control whether access metadata (like creation and modification information) should be automatically logged for a model.

2. Reserved fields

The Reserved fields are predefined fields that Odoo reserves for specific functionality within the system. These fields are also usually added by the Object Relational Mapping (ORM). Reserved fields are required by the Odoo framework for its core operations and cannot be used for other purposes unless they serve the same role. Some common examples include:

* name

This field usually contains the default value as the _rec_name of the model being defined.  ‘name’ is one of the most important and commonly used fields across various models. The name field is often used as the primary string identifier for a model. It is what users see when they reference records in many2one fields, lists, or form views.

* active

This is a boolean field used to make the record ‘archive’ or ‘deactivate’. The field working here is the Odoo.fields.active. If a record has active=False, it effectively becomes "hidden" in most views and reports. The data with active=False will remain in the database forever and the user is able to retrieve the record whenever needed. So, the data will be kept inside the trash until it is deleted permanently.
The active field is defined as
active = fields.Boolean('Active', default=True)

* company_id

While working with a multi-company setup, this reserved field ‘company_id’ comes to the rescue by associating a record in a model with a specific company. This is a Many2one reference field with co-model ‘res. company’ so that only one among the list of the available companies created so far will be saved for each of the records for the model.
Many companies include this field to enforce multi-company rules and data isolation with the syntax.
company_id = fields.Many2one('res.company', string="Company", help='company',
 default=lambda self: self.env.user.company_id)

* state

The ‘state’ field is commonly used to manage the different stages or statuses of a record in a workflow or process. It is often implemented as a selection field where each value corresponds to a different state in the lifecycle of the record. For example, in models like sales orders, purchase orders, and invoices, the state field helps track the progress of a record from draft to completion or cancellation.
Example:
state = fields.Selection([
('draft', 'Draft'),
('confirmed', 'Confirmed'),
('done', 'Done'),
('cancel', 'Cancel')
], string='Status', default='draft')

* parent_id

This allows for the creation of hierarchical relationships. The ‘parent_id’ field is usually a Many2one relationship that links a record to another record of the same model (i.e., self-referencing). 

* parent_path

While developing Odoo models with the earlier kind of hierarchical relationships, the parent_path field is used to represent the full hierarchical path of a record. The parent_path field stores a string that contains the complete path from the top-level parent to the current record, making hierarchical operations (like searching for all children of a specific parent) faster and more efficient.
In short, the Automatic and Reserved fields in Odoo serve the similar purpose of keeping metadata about the Odoo model’s records in the database. These fields are often related to database structure, record management, and system operations. Understanding these fields and usage will help the developers to ensure that the customizations remain compatible with the framework of Odoo and, thereby, maintain integrity.
To read more about What are Automatic & Reserved Fields in Odoo 17, refer to our blog What are Automatic & Reserved Fields in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message