odoo jsonrpc

复制代码
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])

 其他的照搬吧

作者:上官飞鸿

出处:https://www.cnblogs.com/jackadam/p/18017828

版权:本作品采用「知识共享-署名-非商业性-禁止演绎(CC-BY-NC-ND)」许可协议进行许可。

posted @   上官飞鸿  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示