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
import json
import random
import requests
server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
json_endpoint='%s/jsonrpc'%server_url
headers = {"Content-Type":"application/json"}
def get_json_payload(service,method,*args):
return json.dumps({
"jsonrpc":"2.0",
"method":"call",
"params":{"service":service,
"method":method,
"args":args
},
"id":random.randint(0,100000)
})
payload=get_json_payload("common","login",db_name,username,password)
response=requests.post(json_endpoint,data=payload,headers=headers)
user_id=response.json()['result']
if user_id:
print ("success")
else:
print ("failed")
import json
import random
import requests
server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
json_endpoint='%s/jsonrpc'%server_url
headers = {"Content-Type":"application/json"}
def get_json_payload(service,method,*args):
。。。。。。。。
#see full function definition in last section
payload=get_json_payload("common","version")
response=requests.post(json_endpoint,data=payload,headers=headers)
print(response.json())
import json
import random
import requests
server_url='http://127.0.0.1:8069'
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
json_endpoint='%s/jsonrpc'%server_url
headers = {"Content-Type":"application/json"}
def get_json_payload(service,method,*args)
#see full function definition in last section
payload=get_json_payload("common","version")
response=requests.post(json_endpoint,data=payload,headers=headers)
user_id=response.json()['result']
常用方法:read、search、create
if user_id:
#查询数据 ids will be in result keys!
search_domain = ['|',['name','ilike','odoo'],['name','ilike','sql']]
book_ids =get_json_payload("object","execute_kw",db_name,user_id,password,
'library.book','search',
[search_domain],
{'limit':5})
res=requests.post(json_endpoint,data=payload,headers=headers).json()
#read数据 ids will be in result keys!
book_ids =get_json_payload("object","execute_kw",db_name,user_id,password,
'library.book','read',
[res['result'],['name','date_release']])
res=requests.post(json_endpoint,data=payload,headers=headers).json()
json-rpc /创建/更新/删除记录
if user_id:
create_data=[{'name':'test11','release_date':'2020-07-27'},
{'name':'test11','release_date':'2020-07-27'}]
#创建新的记录
payload=get_json_payload("object","execute_kw",db_name,user_id,password,
'library.book','create',
[create_data])
res=requests.post(json_endpoint,data=payload,headers=headers).json()
#修改现有的记录
book_to_write=book_ids[1]
write_data={'name':'book2'}
payload=get_json_payload("object","execute_kw",db_name,user_id,password,
'library.book','write',
['book_to_write','write_data']) #删除现有记录
book_to_delete=book_ids[2:]
payload=get_json_payload("object","execute_kw",db_name,user_id,password,
'library.book','unlink',
['book_to_delete'])
res=requests.post(json_endpoint,data=payload,headers=headers).json()
ODOORPC
import odoorpc
db_name = 'odoo12_202007'
username='admin'
password = 'admin'
odoo=odoorpc.ODOO('localhost',port=8069)
odoo.login(db_name,username,password) #login
user=odoo.env.user
bookModel=odoo.env['library.book']
search_domain = ['|',['name','ilike','odoo'],['name','ilike','sql']]
#查询
book_ids = bookModel.search(search_domain,limit=5)
for book in bookModel.browse(book_ids)
print (book.name,book.date_release)
#创建
book_id = bookModel.create({'name':'test11','release_date':'2020-07-27'})
book=bookModel.browse(book_id)
book.make_available()
book=bookModel.browse(book_id)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章