import json import pprint import random import urllib.request HOST = '192.168.2.21' PORT = 8069 DB = 'wywr17' #数据库名称 USER = '' #登录用户名 PASS = '' #登录密码 def json_rpc(url, method, params): data = { "jsonrpc": "2.0", "method": method, "params": params, "id": random.randint(0, 1000000000), } req = urllib.request.Request(url=url, data=json.dumps(data).encode(), headers={ "Content-Type": "application/json", }) reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8')) if reply.get("error"): raise Exception(reply["error"]) return reply["result"] def call(url, service, method, *args): return json_rpc(url, "call", {"service": service, "method": method, "args": args}) # 登录数据库,获取uid url = "http://%s:%s/jsonrpc" % (HOST, PORT) uid = call(url, "common", "login", DB, USER, PASS) # # 创建记录 creat方法 # args = { # 'name': "张三", # 'create_uid': uid, # } # note_id = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'create', args) # print(note_id) # # 读取记录 read方法 # note_id = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', [1]) # print(note_id[0].get('email')) # # # 更新记录 write方法 # vals = { # 'email': '<EMAIL>', # } # # 老信息 # old_info = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', [1]) # print("old:", old_info[0].get('email')) # # 更新 # func = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'write', [1], vals) # print(func) # # 新信息 # new_info = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', [1]) # print("new:", new_info[0].get('email')) # # # 删除记录 unlink方法 # func = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'unlink', [111]) # print(func) # # # #搜索 search方法 # # 单条件 # ids1 = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', [['is_company', '=', True]]) # pprint.pprint(ids1) # # 多条件 # ids2 = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', [['is_company', '=', True], ['name', '=', '上官飞鸿']]) # pprint.pprint(ids2) # # # 分页 # all_ids = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', []) # pprint.pprint(all_ids) # ten_ids = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', [], 0, 10) # pprint.pprint(ten_ids) # 读取 read方法 # ten_ids=call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', [], 0, 10) # all_rec = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', ten_ids) # # pprint.pprint(all_rec) # for i in all_rec: # print(i.get('name')) # # 读取指定列、字段 # one_rec = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', [1], ['id','name']) # pprint.pprint(one_rec) # ten_ids = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search', [], 0, 10) # ten_rec = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', ten_ids, ['id','name']) # pprint.pprint(ten_rec) # # #列出模型字段,检查可用信息 # # 所有字段 # all_fields = call(url, "object", "execute", DB, uid, PASS, # 'res.partner', 'fields_get', []) # pprint.pprint(all_fields) # # 指定类型字段(str) # str_fields = call(url, "object", "execute", DB, uid, PASS, # 'res.partner', 'fields_get', [], ['string']) # pprint.pprint(str_fields) # # 查找并读取指定字段并分页 search_read方法 # ids = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'search_read', [['is_company', '=', True]], ['id', 'name'], 0, 10) # pprint.pprint(ids)
postman登录,返回用户ID
# # 读取记录 read方法 # note_id = call(url, "object", "execute", DB, uid, # PASS, 'res.partner', 'read', [1])
其他的照搬吧
标签:jsonrpc,uid,url,pprint,DB,call,odoo,PASS From: https://www.cnblogs.com/jackadam/p/18017828