Enable Dark Mode!
By: Mashood K

ORM (Object Relational Mapping) in Odoo


Odoo is a vast ERP stage with immense potentialities. With each version release, the strength and capabilities of the open-source ERP are constantly increasing. In this blog I am going to discuss ORM in Odoo. Before discussing the implications of ORM in Odoo, let’s first look at what is ORM.

What is ORM?

ORM(Object Relational Mapping) is a concept or technique which acts as a bridge between your programming language and your database. The Object Relational Mapping helps to execute SQL queries without writing them explicitly. Once the ORM is configured in an application, the user 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 :

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. So you can simply call the created class properties for it (The setting up of ORM will keep differing based on the programming languages). For example, the above-written SQL commands will look like the following in the ORM concept.

class student:
id: int
name: str
age: int

It will create a relation named ‘student’ the same as the above. The advantage of the above condition is, its reusable. If you want to store some students record in the student table, we can create objects for the class student.

student1 = student()
student2 = student()
student3 = student()
student4 = student()
student5 = student()

Here, each of these objects represents a row in the student table.
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.

Advantages of ORM

- Code reusability
- Accelerates development and avoids writing repetitive SQL codes.
- Reduces development time and cost.
- Provides free space to create a customized pattern rather than the database structure.
- Makes way for developers to focus on coding rather than wasting time on CRUD (Create Read Update Delete) logic.
- Entities are cached in memory, thereby reducing the load on the database.

Odoo ORM

Odoo is considered the best open ERP platform in the market. Odoo is known for its managerial capacity and intuitiveness to perform determined actions of the business. Being open-source, a large community of developers works on the Odoo framework at a time. And for this reason, it is important that you have a well-structured ORM to make SQL operations.

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.

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.

Beneath listed are the Odoo ORM features :
> Traditional and Record styles of APIs.
> Recordset and its operations.
> Environment
> Model class in Odoo
> Methods decorators in Odoo
> Fields and its properties
> Inheritance in Odoo
> Traditional and Record styles of APIs.

Odoo uses two different API styles- ‘traditional’ and ‘record’ for their various versions. These two API styles are separated based on ‘how do they pass the parameters.’ The traditional method passes the parameters like cr, uid, context, etc explicitly. 
Whereas the record style Odoo passes the above-mentioned parameters implicitly,  and the programmers need not pass them in the argument list. Those parameters will remain hidden. This approach gives a more object-oriented feel.

Recordset and its operations.
A Recordset is a set of records of the same Model. It can be a single record or collection of records. We can use decorators like ‘@one’, ‘@multi’ etc to control the records in a recordset. Odoo allows manipulating data in a recordset using different built-in methods. These functions return the filtered values of the current recordset. Major operations are :
1. filtered()
2. sorted()
3. mapped()

Environment stores major data used by the Odoo ORM API. The above-mentioned cr, uid is managed by the Environment. It also stores caches from the users to make a smooth working experience. 

Model class in Odoo
Odoo has various predefined classes (super classes). The model class is one among them.  This class is mainly used for database related Odoo modules.

A major attribute of the model class are : 
1. _name
2. _rec_name
3. _inherit
4. _inherits
5. _order
6. _auto
7. _sql_constrains

Major functions of the model class are :
a. create(vals)
b. browse([ids])
c. unlink()
d. write(vals)
e. read([fields])

Method decorators in Odoo
Decorators are a powerful tool by python, which allows modifying the behaviour of a function, method, or class. Odoo uses decorators to specify what the undertaking function is and what are the data(recordset) contained. Decorators are regarded as highly useful while considering migration from the old version to the new. Methods written in the traditional API style will be automatically converted to the record style in the newer versions. These decorators are described in the apy.py file. Major decorators are :
1. api.multi
2. api.one()
3. api.model()
4. api.multi()
5. api.depends(arguments)
6. api.constraints(arguments)
7. api.onchange(arguments)
8. api.return(model=””, upgrade=None, downgrade=None)

Fields and their properties
The field contains the definition of the fields. And it will be represented as a column in the database structure.

Inheritance in Odoo
Odoo provides 3 different types of inheriting techniques to make the classes and functions reusable.
1. Inheritance.
2. Extension.
3. Delegation.

These features will be explained in detail in the coming days. Stay tuned

Odoo supports several fields for better data handling with specific options for each type. The fields are mainly categorized into 3 types mainly. Read more about Fields and Parameters in Odoo.

Odoo Training Package
Odoo Implementation Package

If you need any assistance in odoo, we are online, please chat with us.

cybrosys youtube


Leave a comment


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635


Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom


Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.


Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message