加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何编写一个使用OpenERP ORM直接上传到Postgres数据库的Python

发布时间:2020-12-14 16:40:09 所属栏目:Java 来源:网络整理
导读:我需要在 Python中编写一个“独立”脚本,只需使用OpenERP的ORM模块即可将数据库中的sales_tax表上传销售税.我想做的是像下面的伪代码. 有人可以向我提供更多关于以下内容的细节: 1)我需要设置什么sys.path 2)在导入“账户”模块之前需要导入哪些模块.目前当
我需要在 Python中编写一个“独立”脚本,只需使用OpenERP的ORM模块即可将数据库中的sales_tax表上传销售税.我想做的是像下面的伪代码.

有人可以向我提供更多关于以下内容的细节:
1)我需要设置什么sys.path
2)在导入“账户”模块之前需要导入哪些模块.目前当我导入“帐号”模块时,会得到以下错误:
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.

解决方法

如果你想通过网络服务,那么看看 OpenERP XML-RPC Web services

OpenERP Web服务的示例代码顶部工作:

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)

更清楚,你也可以使用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"]

你看到两种方式都很好,但是当你使用客户端lib时,使用xmlrpc代码的代码是较少和容易理解的,你将处理的是较低级别的调用希望这将有助于您.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读