| 1 |
.. _tuto-update-browse-records: |
| 2 |
|
| 3 |
Update data through records |
| 4 |
*************************** |
| 5 |
|
| 6 |
By default when updating values of a record, the change is automatically sent |
| 7 |
to the server. |
| 8 |
Let's update the name of a partner:: |
| 9 |
|
| 10 |
>>> Partner = odoo.env['res.partner'] |
| 11 |
>>> partner_id = Partner.create({'name': "Contact Test"}) |
| 12 |
>>> partner = Partner.browse(partner_id) |
| 13 |
>>> partner.name = "MyContact" |
| 14 |
|
| 15 |
This is equivalent to:: |
| 16 |
|
| 17 |
>>> Partner.write([partner.id], {'name': "MyContact"}) |
| 18 |
|
| 19 |
As one update is equivalent to one RPC query, if you need to update several |
| 20 |
fields for one record it is encouraged to use the `write` method as above :: |
| 21 |
|
| 22 |
>>> partner.write({'name': "MyContact", 'website': 'http://example.net'}) # one RPC query |
| 23 |
|
| 24 |
Or, deactivate the ``auto_commit`` option and commit the changes manually:: |
| 25 |
|
| 26 |
>>> odoo.config['auto_commit'] = False |
| 27 |
>>> partner.name = "MyContact" |
| 28 |
>>> partner.website = 'http://example.net' |
| 29 |
>>> partner.env.commit() # one RPC by record modified |
| 30 |
|
| 31 |
Char, Float, Integer, Boolean, Text and Binary |
| 32 |
'''''''''''''''''''''''''''''''''''''''''''''' |
| 33 |
|
| 34 |
As see above, it's as simple as that:: |
| 35 |
|
| 36 |
>>> partner.name = "New Name" |
| 37 |
|
| 38 |
Selection |
| 39 |
''''''''' |
| 40 |
|
| 41 |
Same as above, except there is a check about the value assigned. For instance, |
| 42 |
the field ``type`` of the ``res.partner`` model accept values contains |
| 43 |
in ``['default', 'invoice', 'delivery', 'contact', 'other']``:: |
| 44 |
|
| 45 |
>>> partner.type = 'delivery' # Ok |
| 46 |
>>> partner.type = 'foobar' # Error! |
| 47 |
Traceback (most recent call last): |
| 48 |
File "<stdin>", line 1, in <module> |
| 49 |
File "odoorpc/service/model/fields.py", line 148, in __set__ |
| 50 |
value = self.check_value(value) |
| 51 |
File "odoorpc/service/model/fields.py", line 160, in check_value |
| 52 |
field_name=self.name, |
| 53 |
ValueError: The value 'foobar' supplied doesn't match with the possible values '['contact', 'invoice', 'delivery', 'other']' for the 'type' field |
| 54 |
|
| 55 |
Many2one |
| 56 |
'''''''' |
| 57 |
|
| 58 |
You can also update a ``many2one`` field, with either an ID or a record:: |
| 59 |
|
| 60 |
>>> partner.parent_id = 1 # with an ID |
| 61 |
>>> partner.parent_id = Partner.browse(1) # with a record object |
| 62 |
|
| 63 |
You can't put any ID or record, a check is made on the relationship |
| 64 |
to ensure data integrity:: |
| 65 |
|
| 66 |
>>> User = odoo.env['res.users'] |
| 67 |
>>> user = User.browse(1) |
| 68 |
>>> partner = Partner.browse(2) |
| 69 |
>>> partner.parent_id = user |
| 70 |
Traceback (most recent call last): |
| 71 |
File "<stdin>", line 1, in <module> |
| 72 |
File "odoorpc/service/model/fields.py", line 263, in __set__ |
| 73 |
o_rel = self.check_value(o_rel) |
| 74 |
File "odoorpc/service/model/fields.py", line 275, in check_value |
| 75 |
field_name=self.name)) |
| 76 |
ValueError: Instance of 'res.users' supplied doesn't match with the relation 'res.partner' of the 'parent_id' field. |
| 77 |
|
| 78 |
One2many and Many2many |
| 79 |
'''''''''''''''''''''' |
| 80 |
|
| 81 |
``one2many`` and ``many2many`` fields can be updated by providing |
| 82 |
a list of tuple as specified in the `Odoo` documentation |
| 83 |
(`link <https://github.com/odoo/odoo/blob/12.0/odoo/models.py#L3178>`_), |
| 84 |
a list of records, a list of record IDs, an empty list or ``False``: |
| 85 |
|
| 86 |
With a tuple (as documented), no magic here:: |
| 87 |
|
| 88 |
>>> user = odoo.env['res.users'].browse(1) |
| 89 |
>>> user.groups_id = [(6, 0, [8, 5, 6, 4])] |
| 90 |
|
| 91 |
With a recordset:: |
| 92 |
|
| 93 |
>>> groups = odoo.env['res.groups'].browse([8, 5, 6, 4]) |
| 94 |
>>> user.groups_id = groups |
| 95 |
|
| 96 |
With a list of record IDs:: |
| 97 |
|
| 98 |
>>> user.groups_id = [8, 5, 6, 4] |
| 99 |
|
| 100 |
The last two examples are equivalent to the first (they generate a |
| 101 |
``(6, 0, IDS)`` tuple). |
| 102 |
|
| 103 |
However, if you set an empty list or ``False``, the relation between records |
| 104 |
will be removed:: |
| 105 |
|
| 106 |
>>> user.groups_id = [] |
| 107 |
>>> user.groups_id |
| 108 |
Recordset('res.group', []) |
| 109 |
>>> user.groups_id = False |
| 110 |
>>> user.groups_id |
| 111 |
Recordset('res.group', []) |
| 112 |
|
| 113 |
Another facility provided by `OdooRPC` is adding and removing objects using |
| 114 |
`Python` operators ``+=`` and ``-=``. As usual, you can add an ID, |
| 115 |
a record, or a list of them: |
| 116 |
|
| 117 |
With a list of records:: |
| 118 |
|
| 119 |
>>> groups = odoo.env['res.groups'].browse([4, 5]) |
| 120 |
Recordset('res.group', [1, 2, 3]) |
| 121 |
>>> user.groups_id += groups |
| 122 |
>>> user.groups_id |
| 123 |
Recordset('res.group', [1, 2, 3, 4, 5]) |
| 124 |
|
| 125 |
With a list of record IDs:: |
| 126 |
|
| 127 |
>>> user.groups_id += [4, 5] |
| 128 |
>>> user.groups_id |
| 129 |
Recordset('res.group', [1, 2, 3, 4, 5]) |
| 130 |
|
| 131 |
With an ID only:: |
| 132 |
|
| 133 |
>>> user.groups_id -= 4 |
| 134 |
>>> user.groups_id |
| 135 |
Recordset('res.group', [1, 2, 3, 5]) |
| 136 |
|
| 137 |
With a record only:: |
| 138 |
|
| 139 |
>>> group = odoo.env['res.groups'].browse(5) |
| 140 |
>>> user.groups_id -= group |
| 141 |
>>> user.groups_id |
| 142 |
Recordset('res.group', [1, 2, 3]) |
| 143 |
|
| 144 |
Reference |
| 145 |
''''''''' |
| 146 |
|
| 147 |
To update a ``reference`` field, you have to use either a string or a record |
| 148 |
object as below:: |
| 149 |
|
| 150 |
>>> IrActionServer = odoo.env['ir.actions.server'] |
| 151 |
>>> action_server = IrActionServer.browse(8) |
| 152 |
>>> action_server.ref_object = 'res.partner,1' # with a string with the format '{relation},{id}' |
| 153 |
>>> action_server.ref_object = Partner.browse(1) # with a record object |
| 154 |
|
| 155 |
A check is made on the relation name:: |
| 156 |
|
| 157 |
>>> action_server.ref_object = 'foo.bar,42' |
| 158 |
Traceback (most recent call last): |
| 159 |
File "<stdin>", line 1, in <module> |
| 160 |
File "odoorpc/service/model/fields.py", line 370, in __set__ |
| 161 |
value = self.check_value(value) |
| 162 |
File "odoorpc/service/model/fields.py", line 400, in check_value |
| 163 |
self._check_relation(relation) |
| 164 |
File "odoorpc/service/model/fields.py", line 381, in _check_relation |
| 165 |
field_name=self.name, |
| 166 |
ValueError: The value 'foo.bar' supplied doesn't match with the possible values '[...]' for the 'ref_object' field |
| 167 |
|
| 168 |
Date and Datetime |
| 169 |
''''''''''''''''' |
| 170 |
|
| 171 |
``date`` and ``datetime`` fields accept either string values or |
| 172 |
``datetime.date/datetime.datetime`` objects. |
| 173 |
|
| 174 |
With ``datetime.date`` and ``datetime.datetime`` objects:: |
| 175 |
|
| 176 |
>>> import datetime |
| 177 |
>>> Purchase = odoo.env['purchase.order'] |
| 178 |
>>> order = Purchase.browse(1) |
| 179 |
>>> order.date_order = datetime.datetime(2018, 10, 18, 8, 18, 56) |
| 180 |
|
| 181 |
With formated strings:: |
| 182 |
|
| 183 |
>>> order.date_order = "2018-11-07" # %Y-%m-%d |
| 184 |
>>> order.date_order = "2018-11-07 12:31:24" # %Y-%m-%d %H:%M:%S |
| 185 |
|
| 186 |
As always, a wrong type will raise an exception:: |
| 187 |
|
| 188 |
>>> order.date_order = "foobar" |
| 189 |
Traceback (most recent call last): |
| 190 |
File "<stdin>", line 1, in <module> |
| 191 |
File "odoorpc/fields.py", line 187, in setter |
| 192 |
value = self.check_value(value) |
| 193 |
File "odoorpc/fields.py", line 203, in check_value |
| 194 |
self.pattern)) |
| 195 |
ValueError: Value not well formatted, expecting '%Y-%m-%d %H:%M:%S' format |
| 196 |
|
| 197 |
:ref:`Next step: Change the user's context <tuto-context>` |