コンテンツへスキップ
メニュー
この質問にフラグが付けられました
9 返信
15059 ビュー

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'})
アバター
破棄
最善の回答

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

アバター
破棄
著作者

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).

著作者

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.

著作者

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.

著作者

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.

著作者

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.

最善の回答

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

アバター
破棄
著作者

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.

関連投稿 返信 ビュー 活動
1
2月 17
12667
2
2月 17
5512
2
7月 25
4161
2
12月 24
7428
2
11月 24
28102