• Welcome to Odoo Professional Consulting Agency

Odoo is an Open-source ERP system whose great performance and importance depend on the flexibility, scalability, and affordability of usage of development. Primarily Odoo is written in Python and JavaScript; it’s highly valued as open-source ERP software.

Odoo ERP offers customized and highly adaptable solutions to businesses for managing their operations.


In this blog, we are going to explore the development aspects of Odoo 18 modules. The term fields are the building blocks of models, showing the data stored in the system.


What Are Automatic & Reserved Fields In Odoo 18

These fields define the structure of data in Odoo models and are used to create forms and views and manage business logic. Fields refer to the attributes or properties of a mapped class that correspond to columns in a database table.

Generally, the terms automatic fields and reserved fields refer to the fields that the system manages internally for various functions in Odoo 18.

Developers don’t have to create these fields manually, as they can be generated automatically based on the Odoo model definitions or predefined fields that Odoo reserves for specific purposes.

Understanding Fields in Odoo

In Odoo, fields are key components that define the structure of data within models. A field in Odoo shows specific data similar to a column in a database table.

In Odoo 18, each field consists of multiple fields, which are used to store and manage models related to data.


In the Odoo framework, fields play an important role:

  • Store data : In stored data, fields act as containers and allow the system to store information in the database.
  • Process data : Process data fields are used in validations, calculations, and presenting information to users in views and forms.
  • Interact with the ORM : The ORM, as an object-relational mapping system, uses fields to map Python objects to database records, enabling smooth interaction between the application and the underlying database.

What are automatic fields?


In Odoo, automatic fields are fields that we don't need to define during model definition; they automatically generate when you create the model.


Generally, these fields are added by ORM mapping, which is the bridge between the programming language and the database.


List common automatic fields like:


create_uid: It stores the user ID who created the record. It will automatically save the last users who created the record in the field. It’s a Many2One field with the co-model set as 'res.users’. 


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. Hence, the user cannot change it since it is added automatically.


write_uid: The ID of the last user to modify the record is stored. This system field, write_uid, is also a Many2One field that uses 'res.users’ as a co-model.


This automated field is used to track the most recent update to the record and store the data, such as the user who made the most recent changes.


It has proven useful for auditing and debugging, and it may be used to establish access control by limiting record updates to specific users.


write_date: Stores 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.


Example:


Now we define a field name as ‘name’


from odoo import models,fields

class Example(models.Model):

_name = "my.model.example"

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


Create a record of this model



These fields are automatically added to each model or record, allowing the system to track who did the modification, including when those changes were made.


log_access:


In the Odoo module, the log_access field is a special attribute used at the model level to enable or disable the automatic logging of access metadata, like creation and modification timestamps, with the user who performed these actions. Odoo logs this data by default for all models, but you can simply disable it by setting log_access = false.


In case you are creating a model without disabling log_access, Odoo will automatically include the fields create_uid, create_date, write_uid, and write_date in the database. Hence, it allows developers to control whether access metadata should be automatically logged for a model.


What are Reserved Fields?


Generally, reserved fields are predefined fields that Odoo reserves for specific fields within the system. These fields are usually added by the ORM (Object Relational Mapping).

You can use reserved fields only for its core operations, and they cannot be used for other purposes unless they serve the same task. Here are some examples:


Examples of reserved fields and their functions include:


  • id: It has a unique identifier for each record and supports multi-company features. It refers to a field type that shows many2one reference fields used to associate a record in a model with an individual or a specific company.
  • In the multi-company use-case, the company_field is used. When a user logs into a separate A and B company, the company_id is replaced with the current company id from res.company.


For Example:

Here the company_id is added by default and create a record in multi-company.




  • display_name: A field reserved for the record's human-readable name. It contains a default value as the _rec_name of the model being defined. ‘Name’ is one of the most important and commonly used fields across different models.


You can use the name field as the primary key identifier for a mode. It is what users see when they reference records in many2one fields, lists, or form views.


For Example:


Here are new field is added in model age


from odoo import models, fields

class Example(models.Model):

_name = "my.model.example"

_log_access = False

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

age = fields.Integer(string="Age")


Now Age field set as _rec_name of the field


from odoo import models, fields

class Example(models.Model):

_name = "my.model.example"

_log_access = False

_rec_name = "age"

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

age = fields.Integer(string="Age")


You can see the record name change in the rec name.



  • Explain the limitations and constraints developers face when working with reserved fields (e.g., they cannot be modified or overridden easily).


Differences Between Automatic and Reserved Fields

Automatic fields and reserved fields serve a different purpose in Odoo modules. Hence, it's important to understand their differences.


Functionality: 


  • Automatic Fields: As the name suggests, the field is automatically created and updated by Odoo; for example, fields like create_date and write_uid are added by the system to track the important data.
  • Reserved Fields: Reserved fields are predefined that Odoo sets aside for internal use. Fields like id and display_name are important for Odoo’s core features.


Customization:


  • Automatic Fields: For custom fields Odoo developers work with or modify these fields easily, like using them in custom views for reports. However, Odoo has control over when and how they are updated.
  • Reserved Fields: An Odoo expert cannot change or customize reserved fields, as they play an important role in Odoo’s internal processes. Any modification to them can result in an issue with the system’s functionality.


Overall, automatic fields are created to track any changes made in the record field, while reserved fields are primary elements Odoo uses internally and shouldn’t be modified.


How Automatic and Reserved Fields Impact Development


  • Automatic fields Odoo features that automatically track when records are created or updated, such as create_date and write_uid, are helpful to developers. This eliminates the need for manual coding and makes it simpler to maintain a history of changes. These fields can be used by developers to enhance insight into business activities in workflows or bespoke reports.
  • Reserved fields, including id and display_name, are required for Odoo's internal functionality. Because they are used by the system, developers should avoid modifying or overriding these fields. Customizing reserved fields can result in mistakes and system instability, so they should be left as is.


Best Practices for Working with Automatic & Reserved Fields


It is recommended to use automatically generated fields, such as create_date and write_uid, exactly as they are for tracking record modifications. They can be incorporated into workflows, filters, and reports without requiring any changes to their functionality. Allow Odoo to take care of their upgrades on its own.


Avoid changing reserved fields like id and display_name since they are crucial to Odoo's internal functions. Modifying these fields may damage essential system features or create errors, so leave them alone.


Conclusion


Automatic fields in Odoo 18 modules create automatic fields to track information like the last update date or the creation date of a record. These automatically updated fields make it easier to track record changes without requiring manual effort.


Reserved fields are pre-defined by Odoo 18. It is required for internal system processes, such as the id field, which uniquely identifies the east record. These fields should not be modified or customized because they are important to Odoo’s smooth operations.


To guarantee proper use and prevent system faults, developers must have a detailed understanding of these fields. Odoo developers can securely and effectively expand and adapt Odoo 18 Open-source ERP system by providing the best practices, which include treating reserved fields as reserves and using automatic fields for tracking.


There are no comments for now.

Protected by reCAPTCHA, Privacy Policy & Terms of Service apply.
Sign in to leave a comment
Form View Attributes In Odoo 18
Form View Attributes In Odoo 18