Chapter 3 - Odoo 15 Development Book

Adding models

In Odoo, A model is a class that corresponds to a data relationship (table). It includes all of the necessary fields and behaviors for the data you'll be storing. In the vast majority of cases, each model is associated with a single database table.

There are mainly three types of models.

  • 1. Abstract model
  • 2. Transient model
  • 3. Models.

Models

Model fields are defined as attributes on the model itself:

from . import models, fields
class ExampleModel(models.Model):
	_name = "model name"
	Field = fields.Char(string="fields label")

Abstract Model

Main super-class for regular database-persisted Odoo models.

All Odoo models are created by inheriting from this class:

from . import models, fields
class AbstractModel(models.AbstractModel):
   _name = "model name"
   Field = fields.Char(string="field label")

Transient model

Model super-class for transient records that are intended to be only temporarily persistent and vacuum-cleaned on a regular basis.

The management of access rights has been simplified using a temporary paradigm. All users have the ability to create new records. However, they can only access the records that they have produced. All Transient Model records are accessible to the superuser without restriction.

from . import models, fields
class TransientModel(models.Transientmodel):
	_name = "model name"
	Field = fields.Char(string="field label")

Creating models

To create a new model, we need to add a python file describing it and then upgrade the module. The paths that are used are relative to our add-on module’s path.

1. Add a python file to the models named demo.py

from odoo import models, fields
class DemoClass(models.Model):
	_name = "demo.class"
	name = fields.Char("Name")
	date = fields.Date("Date")

2. Add a python initialization file to be loaded the models that are the __init__.py file

from . import demo

3. Edit the module’s python initialization file to have the models directory loaded by the module

from . import models

4. Upgrade the odoo module from the apps menu in the user interface. We can check if the model is added. Activate the developer mode and go to General Settings > Technical > Database Structure> models search for our model “demo.class” 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