Odoo13_外部API应用

1.配置

# 导入模块
import xmlrpc.client
# 定义 Odoo服务url地址的,数据库名,登录账号,密码
url, db, username, password = "http://www.xxx:8069", "xxx", "xxx, "xxx"

common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
common.version()

# 获取Odoo的res.users表的id
uid = common.authenticate(db, username, password, {})

models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

2.检查权限

# 检查是否有操作的权限 增删改查
res = models.execute_kw(db, uid, password,
        'res.partner', 'check_access_rights',
        ['read'], {'raise_exception': False})
# 返回True或False
print(res)

3.查询

# 获取记录数量
res = models.execute_kw(db, uid, password,
        'res.partner', 'search_count',
        [[['is_company', '=', True]]])
print(res)

# 获取记录id
res = models.execute_kw(db, uid, password,
        'res.users', 'search',
        [[['wxwork_id', '=', 'WangDianChao']]])
print(res[0])

# 多条件查询 获取记录id
res4 = models.execute_kw(db, uid, password,
        'em_wwchat_info', 'search',
        [[['wxwork_id', '=', 'WangDianChao'], ['company_wwchect', '=', 3]]])
print(res4)

# 根据查询到的记录id获取记录中的内容
res5 = models.execute_kw(db, uid, password,
        'em_wwchat_info', 'read',
        [res4], {'fields': ['name', 'email', 'avatar']})
print(res5)

4.创建

# 创建记录
wwchat_info_id = models.execute_kw(db, uid, password, 'wwchat_info', 'create', [{
    'customer_number': "1",   # 要添加的字段 以及内容
    'em_wwchat_info': "2222", 
    'ww_union_id': "3333333", 
    'wwid': "4654654654",
    'remark': "hhlk"
}])
# 返回创建的记录id
print(wwchat_info_id)
原文地址:https://www.cnblogs.com/wangdianchao/p/14473391.html