Odoo Help

1

How to call a method from other class?

By
Juan Formoso
on 12/9/14, 5:11 AM 5,658 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?

can you please give my_method definition also?

Baiju KS
on 12/9/14, 5:28 AM
1
Akhil P Sivan
On 12/9/14, 5:40 AM

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.

1

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.

Juan Formoso
on 12/9/14, 7:03 AM
1

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?

Juan Formoso
on 12/9/14, 6:48 AM

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

Atchuthan - Technical Consultant, Sodexis Inc
on 12/9/14, 7:01 AM
1
Bole
On 12/9/14, 5:37 AM

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.

Juan Formoso
on 12/9/14, 6:51 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 12/9/14, 5:11 AM
Seen: 5658 times
Last updated: 3/16/15, 8:10 AM