| 1 |
.. _tuto-browse-methods: |
| 2 |
|
| 3 |
Call methods from a Model or from records |
| 4 |
***************************************** |
| 5 |
|
| 6 |
Unlike the `Odoo` API, there is a difference between class methods |
| 7 |
(e.g.: `create`, `search`, ...) and instance methods that apply directly on |
| 8 |
existing records (`write`, `read`, ...):: |
| 9 |
|
| 10 |
>>> User = odoo.env['res.users'] |
| 11 |
>>> User.write([6], {'name': "Dupont D."}) # Using the class method |
| 12 |
True |
| 13 |
>>> user = User.browse(6) |
| 14 |
>>> user.write({'name': "Dupont D."}) # Using the instance method |
| 15 |
>>> user.mapped('company_id.partner_id.child_ids.name') # Another use of instance method |
| 16 |
['Chester Reed', 'Dwayne Newman'] |
| 17 |
|
| 18 |
When a method is called directly on records, their `ids` (here `user.ids`) is |
| 19 |
simply passed as the first parameter. |
| 20 |
This also means that you are not able to call class methods such as `create` |
| 21 |
or `search` from a set of records:: |
| 22 |
|
| 23 |
>>> User = odoo.env['res.users'] |
| 24 |
>>> User.create({...}) # Works |
| 25 |
>>> user = User.browse(1) |
| 26 |
>>> user.ids |
| 27 |
[1] |
| 28 |
>>> user.create({...}) # Error, `create()` does not accept `ids` in first parameter |
| 29 |
>>> user.__class__.create({...}) # Works |
| 30 |
|
| 31 |
This is a behaviour `by design`: `OdooRPC` has no way to make the difference |
| 32 |
between a `class` or an `instance` method through RPC, this is why it differs |
| 33 |
from the `Odoo` API. |
| 34 |
|
| 35 |
:ref:`Next step: Update data through records <tuto-update-browse-records>` |