Accessing records through database queries
The Odoo ORM has limited methods, and sometimes it is difficult to fetch certain data from the ORM. In these cases, you can fetch data in the desired format, and you need to perform an operation on the data to get a certain result. Due to this, it becomes slower. To handle these special cases, you can execute SQL queries in the database. In this recipe, we will explore how you can run SQL queries from Odoo.
How to do it...
You can perform database queries using the self._cr.execute
method:
- Add the following code:
self.flush() self._cr.execute("SELECT id, name, date_release FROM library_book WHERE name ilike %s", ('%odoo%',)) data = self._cr.fetchall() print(data) Output: [(7, 'Odoo basics', datetime.date(2018, 2, 15)), (8, 'Odoo 11 Development Cookbook', datetime.date(2018, 2, 15)), (1, 'Odoo 12 Development Cookbook', datetime.date(2019, 2, 13))]
- The result of the...