Now that I've migrated all my python stuff on a module under Odoo I would like to clean a bit more my code starting to pull out all the lines that are writing to others models using self.env.cr.execute, not secure as a native self.write.
I' ve read the docs and I' ve seen there is a way to call another recordset using self.env.context but I did not find a clear solution to write from a model to another.
For example in a model A I need to write in model B with
sql_record_cst = 'INSERT INTO modelB (curr_day, ord_proc) values(%s, 1)'
self.env.cr.execute(sql_record_cst,[self.fieldinmodelA])
self.env.cr.commit()
What should be the equivalent solution using ORM?
First thing of, I should change the target model, then I should be able to change the value of the field with a simple self.field=value or self.write({'fieldA' : value, 'fieldB' : value}).
What I' ve tried:
(in pos.config)
if self.sequence_number == 1:
self.env.cr.execute(sql_test_session,[str(self.start_at[0:10]+'%')])
curr_day = self.env.cr.fetchone()
if curr_day is None:
today_d = str(self.start_at[0:10])
self.env['divina.custos'].create({'curr_day':today_d,'ord_proc':1}
But I get the error:
File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_config.py", line 256, in open_session_cb 'config_id': self.id File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_session.py", line 196, in create res.action_pos_session_open() File "/odoo_dv/odoo_dv-server/addons/point_of_sale/models/pos_session.py", line 222, in action_pos_session_open session.write(values) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 3550, in write self._write(old_vals) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 3758, in _write self.recompute() File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5287, in recompute vals = rec._convert_to_write({n: rec[n] for n in ns}) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5287, in <dictcomp> vals = rec._convert_to_write({n: rec[n] for n in ns}) File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5186, in __getitem__ return self._fields[key].__get__(self, type(self)) File "/odoo_dv/odoo_dv-server/odoo/fields.py", line 872, in __get__ value = record._cache[self] File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5534, in __getitem__ value = self._recs.env.cache[field][self._recs.id]
KeyError: 3
And if I try to force the field date (test purpouse) with
self.env['divina.custos'].create({'curr_day':'2017-02-21 00:00:00','ord_proc':1})
I get another error
File "/odoo_dv/odoo_dv-server/odoo/models.py", line 5534, in __getitem__ value = self._recs.env.cache[field][self._recs.id]KeyError: 4So I think I' m on the right path, I just need to learn the correct syntax.
Please note that
self.env[model.name'].search or self.env['model.name'].browse
works juts fine.