Adding Models

A model is a key idea in Odoo 18's framework that represents a business object. It tells the database how to deal with that object's data fields and business logic. Whereas we can say that a model in Odoo is a Python class that inherits from the models.Model. It maps to a database table and manages records (rows) in that table.

There are three main types of models, each serving a different purpose in the application logic:

  • Models (Standard (Persistent) Model)
  • Abstract Model (Abstract base model)
  • Transient model (Temporary model)

Models

from odoo import models, fields


class MyModel(models.Model):
   _name = 'my.model'
   _description = 'My Custom Model'


   name = fields.Char(string="Name")
   active = fields.Boolean(default=True)
  • The most commonly used model type in Odoo and data is stored persistently in the database.
  • Model fields are described as the model's attributes.
  • Use Case: For any object that needs to be saved, queried, and used throughout the system.

Abstract Model

from odoo import models


class MailThread(models.AbstractModel):
   _name = 'mail.thread'
   _description = 'Email Thread'


   # Adds chatter, messages, and followers to any model
  • Acts as a template or base class for other models.
  • Use Case: Creating reusable logic or behaviors across multiple models.

Creating reusable logic or behaviors across multiple models.

Transient Model

from odoo import fields, models


class ReportWizard(models.TransientModel):
   _name = 'report.wizard'
   _description = 'Report Wizard'


   date_from = fields. Date(string='Start Date',help=”from date.”)
   date_to = fields. Date(string='End Date',help=”To date.)
  • This is where temporary data goes, like data from tools, pop-up forms, or sessions.
  • Records are erased by themselves after a short time.
  • Used to share methods or fields across multiple models.

Creating models

You must first generate a Python file that defines the model class before you can add a new model to Odoo.

The proper Odoo base model, such as models, should be the class's ancestor. Models for models that are persistent.

For short-term models (such as wizards), use TransientModel.

Reusable logic component abstract model. The path of this file is relative to the root of your custom module, and it is usually located inside the models directory.

After you've defined the model, you must upgrade the module—either through the Odoo interface or from the command line — to register it and, if necessary, construct the associated database structures. Without this upgrading step, Odoo will not recognize or apply the new model's configuration.

Steps:

1. In the model directory, add a Python file.

As an example, let's consider developing a Visa Management module. Within this module, we will include a Python file named visa_application.py to define the visa application model.

from odoo import models, fields


class VisaApplication(models.Model):
   """Model for the visa application"""
   _name ="visa.application"
   _description="Visa Application"


   name = fields.Char(string="Name",help="Name of the customer.")

2. Load the Python file into the Python initialization file that has been added.

That is, update the __init__.py file with the following code. Like from . import file_name

from. import visa_application

3. Modify the Python initialization file for the module so that it loads the models directory.

from . import models

4. Upgrade the Odoo module from the applications menu in the user interface.

We can check if the model has been added by activating developer mode and navigating to General Settings > Technical > Database Structure> models and searching for our model "demo.model" on that.

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