Friday, 12 December 2014

XML-RPC web services in OpenERP

XML-RPC is known as a web service. Let's understand how's work in OpenERP.


import xmlrpclib

username = 'admin' #the login user name
pwd = 'admin'      #the password of the user
dbname = 'my_db'    #the database name

# Connection String ........

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

#create partner record

partner = {
   'name': 'Odedra',
   'lang': 'en_US',
}

partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner) #partner is created

print "\n\n===Partner Create id => ", partner_id

#how to update partner record?

vals = {'street': 'Chhaya Road',
   'zip': '360575',
   'city': 'Porbandar',
   'phone': '+2244202',
   'fax': '+2244202222',
}

update_data = sock.execute(dbname, uid, pwd, 'res.partner', 'write', partner_id, vals) #record update successfully

print "\n\n===Is Partner updated? => ", update_data

#how to read partner record?

fields = ['name', 'city', 'zip']

read_data = sock.execute(dbname, uid, pwd, 'res.partner', 'read', partner_id, fields) #read data successfully

print "\n\n===Yes, Here is value for Partner record => ", read_data

#how to search a partner record?

query = [('name', 'ilike', 'Odedra')] #query clause
ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', query) #give the id of searched record otherwise it's return empty list

print "\n\n===how many partner founds? => ", len(ids)
print "\n\n===What their id? => ", ids

#how to call method?
#NOTE: First we have too method on res.partner object than and than we can call that method

res = sock.execute(dbname, uid, pwd, 'res.partner', 'method_name', [partner_id])

print "\n\n===Is method called? => ", res

#how to delete partner record?

result = sock.execute(dbname, uid, pwd, 'res.partner', 'unlink', partner_id) #delete the partner

print "\n\n===Is Partner deleted ? => ", result

print "\n\nGoodBye...."

I hope you like this article. Share your views to improve content. Happy Learning !!!

Youtube Video

No comments:

Post a Comment

ImportError: No module named googleapiclient

Recently, I have faced ImportError: No module named googleapiclient and I would like to share knowledge to fix that problem. Open termina...