File: tuto_context.rst

package info (click to toggle)
python-odoorpc 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 604 kB
  • sloc: python: 3,461; makefile: 154; sh: 36
file content (51 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
.. _tuto-context:

Change the user's context
*************************

`Odoo` uses the user's context to adapt the results of some queries like:
   - reading/updating the translatable fields (english, french...),
   - reading/updating the date fields following the user's timezone
   - change the behavior of a method/query according to the context keys

Global context
''''''''''''''

Changing this context can be done globally by updating the
:func:`context <odoorpc.env.Environment.context>`:

.. doctest::

   >>> odoo.env.context['lang'] = 'en_US'
   >>> odoo.env.context['tz'] = 'Europe/Paris'

From now all queries will be performed with the above updated context.

Model/recordset context
'''''''''''''''''''''''

The context can also be updated punctually with the
:func:`with_context <odoorpc.models.Model.with_context>` method on a model or
a recordset (without impacting the global context).
For instance to update translations of a recordset:

.. doctest::

   >>> Product = odoo.env['product.product']
   >>> product_en = Product.browse(1)
   >>> product_en.env.lang
   'en_US'
   >>> product_en.name = "My product"  # Update the english translation
   >>> product_fr = product_en.with_context(lang='fr_FR')
   >>> product_fr.env.lang
   'fr_FR'
   >>> product_fr.name = "Mon produit" # Update the french translation

Or to retrieve all records (visible records and archived ones):

.. doctest::

   >>> all_product_ids = Product.with_context(active_test=False).search([])


:ref:`Next step: Download reports <tuto-download-report>`