直接在终端中运行该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()