Execute raw SQL queries

Most of the time, you can perform the operations you want using the search() method. However, sometimes, you need more—either you cannot express what you want using the domain syntax, for which some operations are tricky if not downright impossible, or your query requires several calls to search(), which ends up being inefficient.

This recipe shows you how to use raw SQL queries to read res.partner records grouped by country.

Getting ready

We will be using a simplified version of the res.partner model:

class ResPartner(models.Model): _name = 'res.partner' name = fields.Char('Name', required=True) email = fields.Char('Email') is_company = fields.Boolean('Is a company') parent_id = fields.Many2one('res.partner', 'Related Company') ...

Get Odoo Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.