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

I've created two new models: event.meeting and event.meeting.registration (this one has a column named meeting_id, which relates it to the first one).

In event.meeting, I've created a method named my_method (non-static). Now, I want to call it from a function of event.meeting.registration. And to do that, I did this:

for record in self.browse(cr, uid, ids, context=context):
    eve_mee_obj = self.pool.get('event.meeting')
    elements = eve_mee_obj.browse(cr, uid, [record.meeting_id], context=context)
    for element in elements:
        a = element.my_method(cr, uid, [record.meeting_id], context)
return False

But I'm getting this error:

TypeError: my_method() takes at most 5 arguments (9 given)

Can anyone help me, please?

アバター
破棄

can you please give my_method definition also?

最善の回答

If you are trying in odoo API:

You just need to call:

a = element.my_method()

Also in your code, a change is at "for element in elements:", not element in element, please change that also and try.

It is giving you a TypeError like that because, those arguements which you passing in that method call is passing implicitly in new API.

You can also get this in single line as using new API:

a = self.env['event.meeting'].browse([record.meeting_id.id]).my_method(). But then you don't need to use self.pool.get. If you want I can rewrite the code. You just try first one and if not getting, I will help you.

アバター
破棄
最善の回答

Hi,

There is two way of method calling.

1) Using simple class object.

2) Using browse object of that class.

You are calling your method using 2nd way. So, you do not have to pass the cr, uid, ids. You just need to pass context if it is available into method defination.

So, you have to call your method like below.

element.my_method(context=context) or element.my_method()

I hope it helpful to you.

アバター
破棄
著作者

Thank you! I've got context, but I get the error if I try to pass it. Without any parameter, it works.

最善の回答

try this

for record in self.browse(cr, uid, ids, context=context):
    eve_mee_obj = self.pool.get('event.meeting')
    elements = eve_mee_obj.browse(cr, uid, [record.meeting_id], context=context)
    for element in elements:
        a =
eve_mee_obj.my_method(cr, uid, [record.meeting_id], context)
return False

 

In OpenERP 7, you have to call the function from object instantiation.
But this code works for Odoo 8 as function can be called from Object instance and record value.

アバター
破棄
著作者

Thank you! But that will only work if my method were static, won't it?

Is your working environment Version 7 or 8? If it is version 8, then check the odoo documentation for defining methods

最善の回答

1. never fetch objects from within the loop...
--> always self.pool.get BEFORE the loop (inside loop will fetch the same object for every pass in loop)
2. [record.meeting_id] will be browse object
--> maybe you ment: [record.meeting_id.id] (wich will be actual id(s) of object)
3. elements got from eve_mee_obj is in fact one single element (providing single record.meeting_id -> no need for second for loop ) 
-> but do you realy need to browse it if you want to run a method from it?
so try like this:

eve_mee_obj = self.pool.get('event.meeting')
for record in self.browse(cr, uid, ids, context=context):

    a = eve_mee_obj.my_method(cr, uid, [record.meeting_id.id], context)
    # do something with a for each record

 

hope it helps : ) 

 

アバター
破棄
著作者

You're right, I had the pool inside the for and that's not a good idea. However, the method isn't static, so I can't call it from the class. I need to call it from the object. It can be done doing what Akhil P Sivan said. Anyway, thank you very much, your comment is useful.