| 1 |
.. _tuto-context: |
| 2 |
|
| 3 |
Change the user's context |
| 4 |
************************* |
| 5 |
|
| 6 |
`Odoo` uses the user's context to adapt the results of some queries like: |
| 7 |
- reading/updating the translatable fields (english, french...), |
| 8 |
- reading/updating the date fields following the user's timezone |
| 9 |
- change the behavior of a method/query according to the context keys |
| 10 |
|
| 11 |
Global context |
| 12 |
'''''''''''''' |
| 13 |
|
| 14 |
Changing this context can be done globally by updating the |
| 15 |
:func:`context <odoorpc.env.Environment.context>`: |
| 16 |
|
| 17 |
.. doctest:: |
| 18 |
|
| 19 |
>>> odoo.env.context['lang'] = 'en_US' |
| 20 |
>>> odoo.env.context['tz'] = 'Europe/Paris' |
| 21 |
|
| 22 |
From now all queries will be performed with the above updated context. |
| 23 |
|
| 24 |
Model/recordset context |
| 25 |
''''''''''''''''''''''' |
| 26 |
|
| 27 |
The context can also be updated punctually with the |
| 28 |
:func:`with_context <odoorpc.models.Model.with_context>` method on a model or |
| 29 |
a recordset (without impacting the global context). |
| 30 |
For instance to update translations of a recordset: |
| 31 |
|
| 32 |
.. doctest:: |
| 33 |
|
| 34 |
>>> Product = odoo.env['product.product'] |
| 35 |
>>> product_en = Product.browse(1) |
| 36 |
>>> product_en.env.lang |
| 37 |
'en_US' |
| 38 |
>>> product_en.name = "My product" # Update the english translation |
| 39 |
>>> product_fr = product_en.with_context(lang='fr_FR') |
| 40 |
>>> product_fr.env.lang |
| 41 |
'fr_FR' |
| 42 |
>>> product_fr.name = "Mon produit" # Update the french translation |
| 43 |
|
| 44 |
Or to retrieve all records (visible records and archived ones): |
| 45 |
|
| 46 |
.. doctest:: |
| 47 |
|
| 48 |
>>> all_product_ids = Product.with_context(active_test=False).search([]) |
| 49 |
|
| 50 |
|
| 51 |
:ref:`Next step: Download reports <tuto-download-report>` |