How to call a function of a OpenERP class through openerplib or xmlrpclib?

533 Views Asked by At

I'm trying to call a function declared in the class res.partner from a controller. This controller is "outside", so I had to use openerplib (xmlrpclib is also valid) to get data from the models.

For example:

connection = openerplib.get_connection (hostname="localhost", database="my_db", login="admin", password="my_pwd", port=8069)     
res_partner_model = connection.get_model("res.partner")

Now I have the model and I can use search, read, or search_read methods to get data from the res.partner model.

But, how can I call a function declared in that class? Is it possible?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes you can call function using xmlrpclib.

Try this,

import xmlrpclib

#dbname = "my_db"
#username = "admin"
#pwd = "my_pwd"

sock_common = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
#uid = sock_common.login(dbname, username, pwd)
uid = sock_common.login("my_db", "admin", "my_pwd")
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')

#this will search all ids of res_partner class
partner_ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search',[])

for partner_id in partner_ids:
    #res = sock.execute(dbname, uid, pwd, 'res.partner', 'your_function_name', [partner_id])
    res = sock.execute("my_db", uid, "my_pwd", 'res.partner', 'your_function_name', [partner_id])
    print '\n====res=====',res

print " Ending Query ... "