odoo12里面的RPC【远程过程调用】

odoo的RPC有两种:RPC API:1、xml-rpc

                                                     2、json-rpc

案例   xml-rpc

from xmlrpc import client


server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
common=client.ServerProxy('%s/xmlrpc/2/common'%server_url)
user_id=common.authenticate(db_name,username,password,{})
if user_id:
   print ("success:User id is:",user_id)
else:
   print ("Failed:worng credentials")   

user_id=common.authenticate(db_name,username,password,{})

odoo在/xmlrpc/2/common 端点上提供XML-RPC  该方法是一个公共方法,可以被公开调用

authenticate接受四个参数:数据库名,用户名,密码,用户代理环境【非强制参数,无可传空字典】

返回的是:odoo的版本
version_info = common.version()
案例2
from xmlrpc import client

server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
common=client.ServerProxy('%s/xmlrpc/2/common'%server_url)
models=client.ServerProxy('%s/xmlrpc/2/object'%server_url)
user_id=common.authenticate(db_name,username,password,{})
if user_id:
   search_domain = ['|',['name','ilike','odoo'],['name','ilike','sql']]
   book_ids = models.execute_kw(db_name,user_id,password,
                                'library.book','search',
                                [search_domain],
                                 {'limit':5})
   books_data = models.execute_kw(db_name,user_id,password,
                                'library.book','read',
                                ['books_ids',['name','date_release']])
else:
   print ("Failed:worng credentials")
xml-rpc /创建/更新/删除记录

from xmlrpc import client


server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
common=client.ServerProxy('%s/xmlrpc/2/common'%server_url)
models=client.ServerProxy('%s/xmlrpc/2/object'%server_url)
user_id=common.authenticate(db_name,username,password,{})
if user_id:
   create_data=[{'name':'test11','release_date':'2020-07-27'},
                {'name':'test11','release_date':'2020-07-27'})
   #创建新的记录
   book_ids = models.execute_kw(db_name,user_id,password,
-                               'library.book','create',
                                [create_data]
                                )
  #修改现有的记录
  book_to_write=book_ids[1]
  books_data = models.execute_kw(db_name,user_id,password,
                                'library.book','write',
                                ['book_to_write','write_data'])
  #删除现有记录
  book_to_delete=book_ids[2]
  books_data = models.execute_kw(db_name,user_id,password,
                                'library.book','unlink',
                                ['book_to_delete'])
else:
   print ("Failed:worng credentials")

案例json-rpc

猜你喜欢

转载自www.cnblogs.com/1314520xh/p/13388633.html