odoo rpcxml方法

直接在终端中运行该py文件:

python G:\project\odoo\local\lianxi\mylibrary\models\odoo_authenticate.py

 以下图是代码:

import xmlrpc.client

url = 'http://127.0.0.1:8069'#网址

db = 'odoo14-enterprise'#需要操作的数据库

username = 'admin'#系统登录账号

password = 'admin'#系统登录密码

def common_version():#如果没有def会报错,表示不支持xml-rpc

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

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

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

    if user_id:

        search_domain = ['|', ['name', 'ilike', 'odoo'], ['name', 'ilike', 'sql']]#数据库条件查询写法

        books_ids = models.execute_kw(db, user_id, password,'library.book', 'search',[search_domain],{'limit': 5})

        print('Books ids found:', books_ids) #Books ids found: [9]

        books_data = models.execute_kw(db, user_id, password,'library.book', 'read',[books_ids, ['name', 'date_release']])

        #Books data: [{'id': 9, 'name': 'Odoo 14 Development Cookbook', 'date_release': '2023-03-20'}]

        print("Books data:", books_data)

        # 创建新书

        # create_data = [

        #     {'name': '江槐'},

        #     {'name': '柳青'},

        #     {'name': '伍南航'},

        #     {'name': '北修山'}

        # ]

        # books_ids = models.execute_kw(db, user_id, password,

        #     'library.tag', 'create',

        #     [create_data])

        # print("Books created:", books_ids)

        #查询书籍,修改数据

        search_domain_name = [['name', 'ilike', '江槐']]

        books_id = models.execute_kw(db, user_id, password,'library.tag', 'search',[search_domain_name])

        name_write={'name':'水鬼'}

        written = models.execute_kw(db, user_id, password, 'library.book', 'write', [books_id, name_write])

        print("Books written:", written)

        #查询书籍,并删除

        search_domain_name = [['name', 'ilike', '北修山']]

        books_id = models.execute_kw(db, user_id, password,'library.tag', 'search',[search_domain_name])

        deleted = models.execute_kw(db, user_id, password,'library.tag', 'unlink',[books_id])

        print('Books unlinked:', deleted)

    else:

        print("Failed: wrong credentials")

    common.version()

    print(common.version())

    #{'server_version': '14.0+e-20210430',

    # 'server_version_info': [14, 0, 0, 'final', 0, 'e'],

    # 'server_serie': '14.0',

    # 'protocol_version': 1}

    return common

common_version()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值