| 1 |
|
| 2 |
.. _tuto-execute-queries: |
| 3 |
|
| 4 |
Execute RPC queries |
| 5 |
******************* |
| 6 |
|
| 7 |
The basic methods to execute RPC queries related to data models are |
| 8 |
:func:`execute <odoorpc.ODOO.execute>` and |
| 9 |
:func:`execute_kw <odoorpc.ODOO.execute_kw>`. |
| 10 |
They take at least two parameters (the model and the name of the method to |
| 11 |
call) following by additional variable parameters according to the method |
| 12 |
called:: |
| 13 |
|
| 14 |
>>> order_data = odoo.execute('sale.order', 'read', [1], ['name']) |
| 15 |
|
| 16 |
This instruction will call the ``read`` method of the ``sale.order`` model |
| 17 |
for the order ID=1, and will only returns the value of the field ``name``. |
| 18 |
|
| 19 |
However there is a more efficient way to perform methods of a model by getting |
| 20 |
a proxy of it with the |
| 21 |
:func:`model registry <odoorpc.env.Environment.__getitem__>`, which |
| 22 |
provides an API almost syntactically identical to the `Odoo` server side API |
| 23 |
(see :class:`odoorpc.models.Model`), and which is able to set the user |
| 24 |
context automatically (both for read and write operations):: |
| 25 |
|
| 26 |
>>> User = odoo.env['res.users'] |
| 27 |
>>> User.write([6], {'name': "Dupont D."}) |
| 28 |
True |
| 29 |
>>> odoo.env.context |
| 30 |
{'lang': 'fr_FR', 'tz': False} |
| 31 |
>>> Product = odoo.env['product.template'] |
| 32 |
>>> Product.name_get([25]) |
| 33 |
[[25, '[FURN_8220] Bureau Quatre Personnes']] |
| 34 |
|
| 35 |
To stop sending the user context, use the :attr:`odoorpc.ODOO.config` property:: |
| 36 |
|
| 37 |
>>> odoo.config['auto_context'] = False |
| 38 |
>>> Product.name_get([25]) # Without context, lang 'en_US' by default |
| 39 |
[[25, '[FURN_8220] Four Person Desk']] |
| 40 |
|
| 41 |
.. note:: |
| 42 |
|
| 43 |
The ``auto_context`` option only affect methods called from model proxies. |
| 44 |
|
| 45 |
Here is another example of how to install a module (you have to be logged |
| 46 |
as an administrator to perform this task):: |
| 47 |
|
| 48 |
>>> Module = odoo.env['ir.module.module'] |
| 49 |
>>> module_ids = Module.search([('name', '=', 'purchase')]) |
| 50 |
>>> Module.button_immediate_install(module_ids) |
| 51 |
|
| 52 |
:ref:`Next step: Browse records <tuto-browse-records>` |