@@ -749,9 +749,33 @@ The recommended way to build SQL queries is to use the wrapper object
749749
750750.. autoclass :: odoo.tools.SQL
751751
752- .. automethod :: SQL.join
753752 .. automethod :: SQL.identifier
754753
754+ The :method: `~odoo.api.Environment.execute_query ` allows to flush (see later),
755+ execute and fetch results at once.
756+ To translate model fields into SQL, and build queries, a useful abstraction
757+ is provided in
758+
759+ .. autoclass :: odoo.models.Query
760+
761+ .. automethod :: Query.select
762+ .. automethod :: Query.subselect
763+ .. automethod :: Query.is_empty
764+ .. automethod :: Query.get_result_ids
765+ .. autoproperty :: Query.table
766+
767+ .. example ::
768+
769+ .. code-block :: python
770+
771+ query = Query(self .env[' model' ])
772+ query.add_where(SQL(" %s > %s " , query.table.some_field, ' test' ))
773+ query.select()
774+ # SELECT model.id
775+ # FROM model
776+ # WHERE model.some_field > 'test'
777+
778+
755779 One important thing to know about models is that they don't necessarily perform
756780database updates right away. Indeed, for performance reasons, the framework
757781delays the recomputation of fields after modifying records. And some database
@@ -781,6 +805,11 @@ when flushing.
781805
782806.. automethod :: Model.flush_recordset
783807
808+ .. automethod :: odoo.api.Environment.execute_query
809+
810+ When using the Query builder and ``execute_query ``, model flushes are done based
811+ on the metadata in the ``SQL ``.
812+
784813Because models use the same cursor and the :class: `~odoo.api.Environment `
785814holds various caches, these caches must be invalidated when *altering * the
786815database in raw SQL, or further uses of models may become incoherent. It is
@@ -798,6 +827,13 @@ SQL, but not ``SELECT`` (which simply reads the database).
798827
799828 # invalidate 'state' from the cache
800829 self .env[' model' ].invalidate_model([' state' ])
830+
831+ .. code-block :: python
832+
833+ # get name and country name for all records, flushing and joins are
834+ # done automatically
835+ query = Query(self .env[' model' ])
836+ self .env.execute_query(query.select(SQL(query.table.name, query.table.country_id.name)))
801837
802838 Just like flushing, one can invalidate either the whole cache, the cache of all
803839the records of a model, or the cache of specific records. One can even
0 commit comments