我需要在Python中编写一个"独立"脚本,仅使用OpenERP的ORM模块将销售税上传到数据库中的account_tax表.我想做的是像下面的伪代码.
有人可以提供以下更多详细信息:1)我需要设置什么样的sys.path 2)在导入"account"模块之前我需要导入哪些模块.目前,当我导入"帐户"模块时,我收到以下错误:AssertionError:报告"report.custom"已经存在!3)获取数据库光标的正确方法是什么.在下面的代码中,我只是直接调用psycopg2来获取游标.
如果这种方法不起作用,除了编写XML文件以从OpenERP应用程序本身加载数据之外,是否有人可以提出替代方法.此过程需要在标准OpenERP应用程序之外运行.
PSEUDO代码:
import sys
# set Python paths to access openerp modules
sys.path.append("./openerp")
sys.path.append("./openerp/addons")
# import OpenERP
import openerp
# import the account addon modules that contains the tables
# to be populated.
import account
# define connection string
conn_string2 = "dbname='test2' user='xyz' password='password'"
# get a db connection
conn = psycopg2.connect(conn_string2)
# conn.cursor() will return a cursor object
cursor = conn.cursor()
# and finally use the ORM to insert data into table.
Run Code Online (Sandbox Code Playgroud)
ifi*_*hat 17
如果您想通过Web服务进行操作,那么请查看 OpenERP XML-RPC Web服务
示例代码使用OpenERP Web Services进行顶级工作:
import xmlrpclib
username = 'admin' #the user
pwd = 'admin' #the password of the user
dbname = 'test' #the database
# OpenERP Common login Service proxy object
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
#replace localhost with the address of the server
# OpenERP Object manipulation service
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
partner = {
'name': 'Fabien Pinckaers',
'lang': 'fr_FR',
}
#calling remote ORM create method to create a record
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
Run Code Online (Sandbox Code Playgroud)
更清楚的是,您还可以将OpenERP Client lib 示例代码与客户端lib一起使用:
import openerplib
connection = openerplib.get_connection(hostname="localhost", database="test", \
login="admin", password="admin")
user_model = connection.get_model("res.users")
ids = user_model.search([("login", "=", "admin")])
user_info = user_model.read(ids[0], ["name"])
print user_info["name"]
Run Code Online (Sandbox Code Playgroud)
你看到两种方式都很好但是当你使用客户端lib时,代码更少且易于理解,而使用xmlrpc代理是你将处理的低级调用希望这会对你有所帮助.
| 归档时间: |
|
| 查看次数: |
13641 次 |
| 最近记录: |