This question has been flagged
4 Replies
19127 Views

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?

Avatar
Discard

can you please give my_method definition also?

Best Answer

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.

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Best Answer

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.

Avatar
Discard
Author

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

Best Answer

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

 

Avatar
Discard
Author

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.