Skip to Content
Menu
This question has been flagged
1 Reply
6286 Views

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: 4
So 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.

Avatar
Discard
Best Answer

Federico,

Here are the some of thing which might interest you:

  •  For getting the link/access to other model we use :
    self.env['model.name'] (in new api)
    self.pool.get('model.name') ('in old api)

  • from model A, if you want to create record in model B use:
    self.env['b'].create({'key1':'val','key2':val})

  • from model A, if you want to update some field of record 'x' in model B:
    self.env['b'].write(x, {'key1':'val','key2':val})

Hope it helps!!

Avatar
Discard
Author

Yeah, helped a lot, even if I still have some difficult to understand the syntax like

today_d = str(self.start_at[0:10])

self.env['divina.custos'].create({'curr_day':today_d,'ord_proc':1}

that is giving error. But I' m almost there!

I' ve updated my OP with one more little piece of code.

What error are you getting?

Author

Is on the OP, the most common is

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

Related Posts Replies Views Activity
0
Mar 17
2423
1
Mar 17
11225
5
Jul 20
7841
1
Dec 19
4802
5
Sep 20
61231