Development Book V17: Adding Models

A model is a class that corresponds to a data relation. It will have all of the relevant fields and behaviours for the data you will be storing. The _name attribute is very important because it defines the model's name. In many circumstances, each model is coupled with a single database table.

There are three types of models:

  • Models
  • Abstract Model
  • Transient Model
Models

Model fields are defined as attributes in the model.

from odoo import models, fields


class ExampleModel(models.Model):
    _name = "model.name"

    field = fields.Char(string="Field's label")
Abstract Model

The primary super-class for database-persisted Odoo models.

In Odoo, all models are built by inheriting from this class.

from odoo 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 made access permissions management easier. New records can be created by any user. They can, however, only access the records they have made. The superuser has unrestricted access to all Transient Model records.

from odoo import models, fields 


class TransientModel(models.Transientmodel):
    _name = "model.name"

    field = fields.Char(string="Field label")
Creating models

To add a new model, we must first add a Python file defining 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

As an example, let us develop a visa module and include a python file called visa_application.py in it.

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