Odoo is a vast ERP stage with immense potentialities. With each version release, the extensivity of the open source ERP is constantly increasing. In this blog am going to discuss ORM in Odoo. Before discussing the implications of ORM in Odoo, let’s priorly 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.
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 of repetitive SQL codes.
- Reduces the 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 wasting time for CRUD (Create Read Update Delete) logic.
- Entities are cached in memory, thereby reducing the load on the database.
Odoo is considered as the best open ERP platform in the market. Odoo is known for its managerial capacity and intuitive 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 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.
> 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 :
Environment stores major data used by the Odoo ORM API. The above-mentioned cr, uid are 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.
Major attribute of the model class are :
Major functions of the model class are :
Method decorators in Odoo
Decorators are a powerful tool by the python, which allows modifying the behavior of a function, method or class. Odoo uses decorators to specify what the undertaking function is and what data(recordset) contained is. Decorators are regarded highly useful while considering migration from old version to 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 :
8. api.return(model=””, upgrade=None, downgrade=None)
Fields and its 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.
These features will be explained in detail in the coming days. Stay tuned.