Chapter 15 - Odoo 15 Development Book

Generate records through database query

The Odoo ORM has limited methods to Generate Records , but sometimes it has some difficulties to fetch datafrom the ORM. In these cases, we can fetch data in to the specific format, and you need to perform an operation on the data to get a perticular result. Due to this, it becomes slower. To handle these special cases, you can execute SQL queries in the database. In this case,here we have to check how we can run SQL queries from Odoo.

The Odoo developers feel that running SQL queries instead of ORM queries speeds up operations.This isn't entirely accurate; it depends on the situation.Because data is delivered via the recordset cache, the ORM performs better and faster than RAW queries in most tasks.

There are two ways to access the database from the record. One is using the self._cr , and the other is from the environment, in particular, that is using .

Example for using'SELECT id, name FROM school_student WHERE
name ilike + search_keyword + ';')

Discuss some example how to use self._cr on the python code

self._cr.execute("SELECT id, name, age FROM
school_student WHERE name ilike %s", ('%odoo%',))
data = self._cr.fetchall()

Just print the data we can get record when the name will be have the string odoo.the result will be in the form of tuples or lists.

If we want the result as a dictionary format we can use dictfetchall() method instead of fetchall(), for example

self._cr.execute("SELECT id, name, age FROM
school_student WHERE name ilike %s", ('%odoo%',))
data = self._cr.dictfetchall()

The output will be in the form of a dictionary. And also we can take only a single record we can use fetchone() or dictfetchone() methode.those methode has the same functionality of fetchall() and dictfetchall() ,methods as corresponding.



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



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