Accessing records through database queries
Odoo ORM has limited methods, and sometimes, it is difficult to fetch certain data from 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, room_no, floor_no FROM hostel_room WHERE name ilike %s", ('%Room A-%',)) data = self._cr.fetchall() print(data)
Here is the output:
[(4, 'Room A-101', '101', 1), (5, 'Room A-103', '103', 1), (6, 'Room A-201', '201', 2)]
- The result of the query will be in the form of a list of tuples. The...