Overview of Automatic and Reserved Fields in Odoo

TEAM-BASSAM
July 3, 2023
automatic-and-reserved-fields

In Odoo, some fields are never needed to be defined, which are automatically determined in odoo itself

  • 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 model

For Example:

Now I am creating a model

from odoo import models

class Example(models.Model):

_name = "model.example"

We can see in the database some fields are automatically created

automatic-fields-in-odooHere we can see automatically created fields such as

  • 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 _auto

2.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

  1. Write_uid(store when the record was created)

 This is also a many2one field of model ‘res. users’, which stores the user_id that makes the latest modification in the record. This field is automatically set whenever a record is updated, and it is a system field.

  1. Create_date (stores when the record was created)

This field is a system-defined field in Odoo that is used to store the date and time when a record is created in the database. It is an automatic field that is automatically generated and managed by the system and cannot be modified by the user.

  1. Write_date(Store when the record was last updated)

 This field automatically fills with the last updated or modified date of the record on the model

Example:

 Now we define a field name as ‘name’

from odoo import models,fields


class Example(models.Model):

_name = "model.example"

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

Create a record of this model

create-a-record

In the database, we can see these fields

fields-in-odoo

So here we see

  • 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)

This fields automatically enable the _log_access = True added or nothing mention , If _log_access = False ,the above field to be null

For example

Now I’m setting _log_access =False

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

creating-a-new-recordIn the database we can see _log_access field will be null

log-access-field

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 age

from 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

record-name-change2. 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

Saving-the-record

So this record exists in an Archived state

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

creating-a-record-with-multi-company

Here we can see the database company id of My Company(San Francisco) id will with company_id from res. 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

 

Click again on approve button, it will become active

    1. Model.action_archive()

 Sets active to False on a record. set, by calling Model.action_archive() on its currently active records.
 For example:

def action_approve(self): 

self.action_archive()

Here we can see the recordset is archived

6. Model.action_unarchive()

 Sets active to True on a recordset, by calling Model.action_unarchive() on its currently active records.

For Example:

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

Also, we can add more states we used in selection_add

state = fields.Selection(selection_add=[('approve', 'To Approve')], 

string='status', tracking=True, default='draft')

 
  1. parent_id

 The default value of _parent_name, is used to organize records in a tree structure and enables the child_of and parent_of operators in domains. The parent_id has a type of Many2one and child_of has One2many.

For Example:

Now I’m setting the parent_id as res. partner

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.

 

DOWNLOAD ERP

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *