Debian Codegraph @

doc/source/faq.rst

raw
1 .. _faq:
2
3 Frequently Asked Questions (FAQ)
4 ================================
5
6 Why OdooRPC? And why migrate from OERPLib to OdooRPC?
7 -----------------------------------------------------
8
9 It was a tough decision, but several reasons motivated the `OdooRPC` project:
10
11 **RPC Protocol**
12 The first point is about the supported protocol, `XML-RPC` is kept in `Odoo`
13 for compatibility reasons (and will not evolve anymore, maybe removed one
14 day), replaced by the `JSON-RPC` one. Although these
15 protocols are almost similar in the way we build RPC requests, some points
16 make `JSON-RPC` a better and reliable choice like the way to handle errors
17 raised by the `Odoo` server (access to the type of exception raised, the
18 complete server traceback...). To keep a clean and maintainable base code, it
19 would have been difficult to support both protocols in `OERPLib`, and it is
20 why `OdooRPC` only support `JSON-RPC`.
21
22 Another good point with `JSON-RPC` is the ability to request all server web
23 controllers to reproduce requests (`type='json'` ones) made by the official
24 `Javascript` web client.
25 As the code to make such requests is based on standard `HTTP` related Python
26 modules, `OdooRPC` is also able to request `HTTP` web controllers
27 (`type='http'` ones).
28
29 In fact, you could see `OdooRPC` as a high level API for `Odoo` with which
30 you could replicate the behaviour of the official `Javascript` web client,
31 but in `Python`.
32
33 **New server API**
34 One goal of `OERPLib` was to give an API not too different from the server
35 side API to reduce the learning gap between server-side development and
36 client-side with an `RPC` library. With the new API which appears in
37 `Odoo` 8.0 this is another brake (the old API has even been removed
38 since Odoo 10.0), so the current API of `OERPLib` is not anymore consistent.
39 As such, `OdooRPC` mimics A LOT the new API of Odoo, for more
40 consistency (see the :ref:`tutorials <tutorials>`).
41
42 **New brand Odoo**
43 `OpenERP` became `Odoo`, so what does `OERPLib` mean? `OEWhat`? This is
44 obvious for old developers which start the `OpenERP` adventure since the
45 early days, but the `OpenERP` brand is led to disappear, and it can be
46 confusing for newcomers in the `Odoo` world. So, `OdooRPC` speaks for
47 itself.
48
49 **Maintenance cost, code cleanup**
50 `OpenERP` has evolved a lot since the version 5.0 (2009), making `OERPLib`
51 hard to maintain (write tests for all versions before each `OERPLib` and
52 `OpenERP` release is very time consuming). All the compatibility code for
53 `OpenERP` 5.0 to 7.0 was dropped for `OdooRPC`, making the project more
54 maintainable. `Odoo` is now a more mature product, and `OdooRPC` should
55 suffer less about compatibility issues from one release to another.
56
57 As `OdooRPC` has not the same constraints concerning `Python`
58 environments where it could be running on, it is able to work on `Python`
59 2.7 to 3.X.
60
61 `OdooRPC` is turned towards the future, so you are encouraged to use or migrate
62 on it for projects based on `Odoo` >= 8.0. It is more reliable, better covered
63 by unit tests, and almost identical to the server side new API.
64
65
66 Connect to an Odoo Online (SaaS) instance
67 -----------------------------------------
68
69 First, you have to connect on your `Odoo` instance, and set a password for
70 your user account in order to active the `RPC` interface.
71
72 Then, just use the ``jsonrpc+ssl`` protocol with the port 443::
73
74 >>> import odoorpc
75 >>> odoo = odoorpc.ODOO('foobar.my.odoo.com', protocol='jsonrpc+ssl', port=443)
76 >>> odoo.version
77 '8.saas~5'
78
79 Update a record with an `on_change` method
80 ------------------------------------------
81
82 .. note:
83 It is about the the old API (`on_change` statement declared in a XML view
84 with its associated Python method).
85
86 `OdooRPC` does not provide helpers for such methods currently.
87 A call to an ``on_change`` method intend to be executed from a view and there
88 is no support for that (not yet?) such as fill a form, validate it, etc...
89
90 But you can emulate an ``on_change`` by writing your own function,
91 for instance::
92
93 def on_change(record, method, args=None, kwargs=None):
94 """Update `record` with the result of the on_change `method`"""
95 res = record._odoo.execute_kw(record._name, method, args, kwargs)
96 for k, v in res['value'].iteritems():
97 setattr(record, k, v)
98
99 And call it on a record with the desired method and its parameters::
100
101 >>> order = odoo.get('sale.order').browse(42)
102 >>> on_change(order, 'product_id_change', args=[ARGS], kwargs={KWARGS})
103
104 Some model methods does not accept the `context` parameter
105 ----------------------------------------------------------
106
107 The ``context`` parameter is sent automatically for each call to a `Model`
108 method. But on the side of the `Odoo` server, some methods have no ``context``
109 parameter, and `OdooRPC` has no way to guess it, which results in an nasty
110 exception. So you have to disable temporarily this behaviour by yourself by
111 setting the ``auto_context`` option to ``False``::
112
113 >>> odoo.config['auto_context'] = False # 'get()' method of 'ir.sequence' does not support the context parameter
114 >>> next_seq = odoo.get('ir.sequence').get('stock.lot.serial')
115 >>> odoo.config['auto_context'] = True # Restore the configuration
116
117 Change the behaviour of a script according to the version of Odoo
118 -----------------------------------------------------------------
119
120 You can compare versions of `Odoo` servers with the :func:`v <odoorpc.tools.v>`
121 function applied on the :attr:`ODOO.version <odoorpc.ODOO.version>` property,
122 for instance::
123
124 import odoorpc
125 from odoorpc.tools import v
126
127 for session in odoorpc.ODOO.list():
128 odoo = odoorpc.ODOO.load(session)
129 if v(odoo.version) < v('10.0'):
130 pass # do some stuff
131 else:
132 pass # do something else