Odoo ORM and Models

 ORM (Object Relational Mapping) concept in Odoo



ORM(Object Relational Mapping) is a concept or technique which acts as a bridge between your programming language and your database. Object Relational Mapping helps to execute SQL queries without writing them explicitly.With the help of ORM we can use the OOP concepts like classes and objects to interact with the database. 

For instance,  assume that you want to create a relation(table) named ‘student’ which belongs to the database ‘school’. Simply, you can use the SQL commands to achieve this. Like :

CREATE TABLE student(id int,name varchar(255), age int);


This will create a table named student with 3 columns(id, name, age). But the problem is, if you are a part of a huge project and your application needs to dynamically interact with the database, it is not a smart way to write SQL queries each time. 

There comes the interplay of ‘ORM’. In the ORM, the application you are creating will be interacting (fetching or putting data to the database) with the database through classes and objects.

If you do not use ORM in the application, you will be needing to write SQL queries to add value to the table each time. But in the ORM, you just create an object and calling the class by passing -the required values. It makes your coding reusable and object-oriented.

Odoo ORM

Fortunately,  Odoo encompasses with well-structured ORM API, which provisions the developers to interact with the database via some methods and operations. Odoo uses some tools and packages like psycopg2 to build a proper ORM API

In Odoo Entities are cached in memory, thereby reducing the load on the database.

It starts from the basic CRUD (create, read, update, delete) operations. Meanwhile, it also includes other operations such as data importing to the database, exporting to files, and so forth.


Odoo Model


Odoo models are created by inheriting one of the following:

  • Model for regular database-persisted models

  • TransientModel for temporary data, stored in the database but automatically vacuumed every so often

  • AbstractModel for abstract super classes meant to be shared by multiple inheriting models

When we create a model class and restart the server or update or install the app then it create the db tables automatically. If we don't want to create any db table we can set  _auto = False in model class

If set to False, override init() to create manually the database table.
Automatically defaults to True for Model and TransientModel. False for AbstractModel





Comments