Odoo 16 Development Book

Adding Models

A model is a class that maps to a data relation. It will include all the necessary fields and behaviors for the data you will be storing. The _name attribute is most required as it defines the name of the model. In many cases, every model is associated with a single database table.

Three types of models are:

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

Models

In the model itself, model fields are defined as attributes.

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

Abstract Model

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

All models in Odoo are created by inheriting from this class.

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

Transient model

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

It has simplified access rights management. All users can create new records. But, they can only access the records they have created. Without any restrictions, all Transient Model records are accessible to the superuser.

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

Creating models

In order to create a new model, we should add a python file describing it and then upgrade the module. The path which is used is relative to our add-on module’s path.

1. In the model directory, add a python file

example: Let us create a module named visa in that add a python file named visa_application.py

from Odoo import models, fields
class VisaApplication(models.Model):
	_name = "visa.application"
	name = fields.Char("Name")

2. Add a python initialization file and load the python file to it.

I.e., Add the below code to the __init__.py file

from . import visa_application

I.e., from . import filename

3. In order to have the models directory loaded by the module, edit the module’s python initialization file.

from . import models

4. From the apps menu in the user interface, upgrade the Odoo module. We can check if the model is added by activating the developer mode and going to General Settings > Technical > Database Structure> models and search for our model “demo.model” on that.

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