This question has been flagged

I've started a migration to Odoo 10 from Odoo 8 and I didn't figured out how to replace a simple self.write with the new API.

For example, on one of my custom module I have

self.write(cr, uid, context['active_ids'], {'to_wait':'No'})

but when I fire the server action in my view I get

NameError: name 'self' is not defined

This is my working code for the server action on V8:

<record id="rest_order_send" model="ir.actions.server">
<field name="sequence" eval="5"/>
 <field name="name">rest_order_send</field>
 <field name="model_id" ref="model_rest_order"/>
 <field name="condition">True</field>
<field name="type">ir.actions.server</field>
 <field name="state">code</field>
 <field name="code">self.write(cr, uid, context['active_ids'],{'status':'Pronto'})
self.write(cr, uid, context['active_ids'], {'to_floor':'2'})
self.write(cr, uid, context['active_ids'], {'end_date':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")})
</field>
</record>

Looking on Odoo10 doc I found I can't use self.write  anymore and I have to use another syntax now, but sadly I didn't figured out how to use the new object_write command.


Partially solved with the help of Raaj with:

pos_obj = env['pos.order'].browse(context.get('active_id'))
pos_obj.write({'costs': '1.45'})
Avatar
Discard
Best Answer

Hi Federico, as per new API you can use like,

self.env['object_name']

for more information you can go through these document.

1.https://media.readthedocs.org/pdf/odoo-new-api-guide-line/latest/odoo-new-api-guide-line.pdf

2.https://www.odoo.com/documentation/8.0/reference/orm.html

Avatar
Discard
Author

Raaj thanks for your reply. However the first link you gave me doesn't works. I had a look again on the doc, but self still raising the same error.

self.env['pos.order']

object.write({'field': 'value'})

At this point I don't know if is possible to use self without a def on my Python code (now the action is called trough xml).

Author

Same thing with

self.env['pos.order'].write({'field': 'value'})

Hi Federico,if you are calling the method from the pos.order object then self is enough for the operation like,

self.write({'to_wait':'No'})

But if its from different object then you need to get the id of the pos.order object,as i can see at your code in the question you are having them in context.

so in that case you need to perform like this,

pos_obj = self.env['pos.order'].browse(self._context.get('active_id'))

pos_obj.write({'field': 'value'})

if the length of active_ids is more than 1 then you can loop through the statements.

Author

Raaj, on both my module and pos.order I get always the same error for self:

ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating

u"pos_obj = self.env['pos.order'].browse(self._context.get('active_id'))\n\npos_obj.write({'costs': '1.35'})"

Can you pls post your method for more details and the object name under which its defined.

Author

I've updated my original post with the xml pulled from one of my modules on V8.

Speaking truly, I've inherited the pos.order and pos.order.line, where I've added 2 more fields because the next step, when I'll understand how this API works, is calling a def.

That because I'm migrating part of my python scripts from my daemon inside Odoo but frankly the lack of documentation (with good examples) is discouraging. I mean a simple sql query and an update with psycopg2 took me 5 minutes of works. On Odoo I'm stuck since yesterday.

Author

Raaj,

the issue was solved after a complete remove of 'self' from the line:

pos_obj = env['pos.order'].browse(context.get('active_id'))

pos_obj.write({'costs': '1.45'})

Anyway I'm still not understanding the logic.

Best Answer

In odoo10 for write  function only needed this self.write({'field name':'value'}).

Avatar
Discard
Author

In this specific example (see comments) self will rise an error and the solution I've write on the OP works fine without. So no, it's not always working.