更好地利用odorpc的实用程序
odoorpc-utils的Python项目详细描述
概述
本模块提供以下功能:
从单个.ini文件实例化通用的odoo rpc库(当前支持odooly和odoopc);
运行deep读取和搜索读取查询,从多个 以高效的方式立即建模并使用rpcs的并行执行;
支持deep查询的函数也可以用作构建 块以支持graphql或类似graphql的查询。
待办事项:
- 扩展utils包以支持graphql查询;
- 编写一个odorpc_fast类,通过使用http会话和gzip编码来加速rpc查询;
- 很多,很多潜在的功能…请随时帮助写作和建议!
示例1:
from odoorpc_utils import FastDeepQueries, OdooInstantiator
# read from file odoorpc_utils.ini, odoorpc-utils.ini or odooly.ini
# If you prefer you can skip this and instantiate odoorpc the old way.
odoo = OdooInstantiator.odoorpc_from_config_file('production')
# pass an instance of logged-in odoorpc here (odooly not supported at the moment)
fdq = FastDeepQueries(odoo)
# Let's get the id of 100 sales orders...
ids = fdq.query_fast(model_name='sale.order', limit=100)
# The fields you want to retrieve are specified in this easy to understand syntax...
fields = ['number,date_confirmed,order_total',
'partner_id.name,address,zip,city,state,tax_number',
'sale_order_line_ids.quantity, unit_price, total_price',
'sale_order_line_ids.product_id.name,reference']
# ...which is then parsed, relations are retrieved and a more complex structure of dicts is built
fields_deep = fdq.build_fields_deep_structure(model_name='sale.order', fields_deep=fields)
# Now let's make a deep query for 100 sales order and see how fast it is
# Nope, it will not be blazing fast, it will actually be quite slow, but MUCH faster
# than what you usually get from odoorpc...
res = fdq.query_deep(fields_deep, ids=ids, n_threads=8)
# Done!
# RPC is slow... each query takes ~1 second, larger queries can take much longer. Running them in
# parallel speeds things up significantly (but it is still RPC)
print(res)
例2:
from odoorpc_utils import FastDeepQueries, OdooInstantiator
odoo = OdooInstantiator.odoorpc_from_config_file('production')
fdq = FastDeepQueries(odoo)
# We can also run queries defined on a queries.ini file
res = fdq.named_query('picking list report', ids=[order_id])
示例odorpc\u utils.ini文件:
[DEFAULT]
port = 443
username = user
password = pass
# Odooly-specific
scheme = https
protocol = jsonrpc
#OdooRPC-specific
timeout = 3600
# if **protocol_odoorpc** is missing, the param protocol will be
# used with odoorpc instead. But Odooly and OdooRPC do not support the same
# syntax for the protocol string, hence why an odoorpc-specific option exists
protocol_odoorpc = jsonrpc+ssl
[production]
host = www.my.tld
database = my_production_database
[testing]
host = testing.my.tld
database = testing_database
示例querys.ini文件
[DEFAULT]
# acceptable values are all parameters of the query_deep function
# with the exception of fields_deep that MUST be provided by means of the
# fields *model name* and *fields*.
n_threads = 8
# format_for__id_fields
# 1 for [id, 'Name'] - Standard OdooRPC.
# 2 for [id] - useful to have both _id and _ids fields on list of ids, but all
# _id fields have one id only, so the list is not necessary
# and also the create/write calls do not accept a list with one id.
# 3 for the id as an int - default. Best if you plan to later use the create/write calls
format_for__id_fields = 3
[picking list report]
model name = sale.order
fields = number,date_confirmed,order_total
fields = partner_id.name,address,zip,city,state,tax_number
fields = sale_order_line_ids.quantity, unit_price, total_price
fields = sale_order_line_ids.product_id.name,reference