Blog

News & Events
Overview of Automatic and Reserved Fields in Odoo
July 3, 2023
In Odoo, some fields are never needed to be defined, which are automatically determined in odoo itself
Here we can see automatically created fields such as
In the database, we can see these fields
So here we see
In the database we can see _log_access field will be null
Set an active field on a model as active=Boolen(default=True), if we active field turn into False, this record will archive on the tree view, it is an unarchived state,
For Example:
Now I'm adding a field active. This active field will become false when the function is working
So this record exists in an Archived state
This field supports multi-company functionality. It refers to a field type that represents a many2one reference field used to link a record in a model to a specific company. In the multi-company use case, the company_field is used. When a user Logged in to a different A And B company, the company_id becomes the current company id from res. company.
For Example:
Now I'm adding company_id as default and creating a record in multi-company
Here we can see the database company id of My Company(San Francisco) id will with company_id from res. company
Click again on approve button, it will become active
6. Model.action_unarchive()
Sets active to True on a recordset, by calling Model.action_unarchive() on its currently active records.
For Example:
lifecycle stages of the object, used by the states' attributes on fields.the fields must be selection fields.Each state value is defined as a tuple containing a string label and a unique identifier
For Example:
Now I'm adding state field in our model
Also, we can add more states we used in selection_add
- Automatic Fields
- Reserved Fields
Automatic Fields in Odoo
Automatic fields are fields that are automatically generated by odoo and do not need to To defined explicitly in the modelFor Example:
Now I am creating a modelfrom odoo import models class Example(models.Model): _name = "model.example"We can see in the database some fields are automatically created

- Id
Access Log Fields
- create_uid
- Write_uid
- Create_date
- Write_date
1.Id (identifier)
This field represents a database column that stores the unique identifier (ID) for a record in a model. Each record has its own id as a unique one. It cannot be edited once the record is created. This Identifier field is useful to identify each record as specifically. Request Your Free Quote Access Log Fields These fields are automatically set and updated if _log_access is enabled. It can be disabled to avoid creating or updating those fields on tables for which they are not useful. By default, _log_access is set to the same value as _auto2.Create_uid(user who created the record)
This field is automatically generated by odoo, which means if any of the users created this record, So his “res.user” id will automatically fill in that field by odoo. in The logged user’s id will fill with that field, so we can know who has created this record-
Write_uid(store when the record was created)
- Create_date (stores when the record was created)
-
Write_date(Store when the record was last updated)
from odoo import models,fields class Example(models.Model): _name = "model.example" name = fields.Char(string="Name")Create a record of this model


- Id filled it with unique 1
- Create_uid(Mitchile Admin id from res.user)
- Write_uid(Last updated user id)
- Create_date(date that record has created)
- Write_date(Last updated date of the record)
from odoo import models,fields class Example(models.Model): _name = "model.example" _log_access = False name = fields.Char(string="Name")And now I am creating a new record


Reserved Fields in Odoo
A few field names are reserved for pre-defined behaviors beyond that of automated fields. They should be defined on a model when the related behavior is desired. Different Types of Reserved Fields- name
- active
- company_id
- toggle_active()
- action_archive()
- action_unarchive()
- state
- parent_id
- parent_path
1. Model. name
The default value for _rec_name is used to display records in a context where a Representative “naming” is necessary.it is used to represent the model rec name. For Example: Now I'm adding a new field in our model agefrom odoo import models, fields class Example(models.Model): _name = "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 = "model.example" _log_access = False _rec_name = "age" name = fields.Char(string="Name") age = fields.Integer(string="Age")So here we see the record name change in the rec name
2. Model. active
Set an active field on a model as active=Boolen(default=True), if we active field turn into False, this record will archive on the tree view, it is an unarchived state,
For Example:
Now I'm adding a field active. This active field will become false when the function is working
from odoo import models, fields class Example(models.Model): _name = "model.example" _log_access = False _rec_name = "age" name = fields.Char(string="Name") age = fields.Integer(string="Age") active = fields.Boolean('Active', default=False)Now I'm saving the record

3. Comany_id
This field supports multi-company functionality. It refers to a field type that represents a many2one reference field used to link a record in a model to a specific company. In the multi-company use case, the company_field is used. When a user Logged in to a different A And B company, the company_id becomes the current company id from res. company.
For Example:
Now I'm adding company_id as default and creating a record in multi-company
company_id = fields.Many2one("res.company")On view, creating a record with multi-company


4. toggle_active()
Inverse the value of active on the records in self. This means the record is active, it will be inactive, or vice versa For Example:def toggle_active(self): self.toggle_active()The current record is active and it will become inactive

-
- Model.action_archive()
def action_approve(self): self.action_archive()Here we can see the recordset is archived

def.action_approve(self): self.action_unarchive()Here we can see the recordset is archived
7. Model. state
lifecycle stages of the object, used by the states' attributes on fields.the fields must be selection fields.Each state value is defined as a tuple containing a string label and a unique identifier
For Example:
Now I'm adding state field in our model
state = fields.Selection(selection=[('draft', 'Draft'), ('confirmed', 'Confirmed'), ('cancel', 'cancel'), ], string='Status', tracking=True, default='draft')Here we can see the state fields are created such as draft,confirmed and cancel, if we click on the next stage, that stage will change into

state = fields.Selection(selection_add=[('approve', 'To Approve')], string='status', tracking=True, default='draft')
- parent_id
parent_id = fields.Many2one('res.partner',String="partner")

9. Model.parent_path
When _parent_store is set to True, it is used to store a value reflecting the tree structure of _parent_name, and to optimize the operators child_of and parent_of in search domains. It must be declared with index=True for proper operation. For Example:parent_id = fields.Many2one('res.partner', String="partner",index=True)Index =True means you can speed up the database search performance. However, you should be careful not to add indexes on too many fields because the size of the database can rapidly increase. This means you should carefully detect the most searchable fields and create indexes for them only.
