How to write a Python script that uses the OpenERP ORM to directly upload to Postgres Database

让人想犯罪 __ 提交于 2019-12-09 04:30:55

问题


I need to write a "standalone" script in Python to upload sales taxes to the account_tax table in the database using ONLY the ORM module of OpenERP. What I would like to do is something like the pseudo code below.

Can someone provide me a more details on the following: 1) what sys.path's do I need to set 2) what modules do I need to import before importing the "account" module. Currently when I import the "account" module I get the following error: AssertionError: The report "report.custom" already exists! 3) What is the proper way to get my database cursor. In the code below I am simply calling psycopg2 directly to get a cursor.

If this approach cannot work, can anyone suggest an alternative approach other than writing XML files to load the data from the OpenERP application itself. This process needs to run outside of the the standard OpenERP application.

PSEUDO CODE:

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.

回答1:


If you wanna do it via web service then have look at the OpenERP XML-RPC Web services

Example code top work with 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)

More clearly you can also use the OpenERP Client lib Example Code with client 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"]

You see both way are good but when you use the client lib, code is less and easy to understand while using xmlrpc proxy is lower level calls that you will handle Hope this will help you.




回答2:


As per my view one must go for XMLRPC or NETSVC services provided by Open ERP for such needs.

You don't need to import accounts module of Open ERP, there are possibilities that other modules have inherited accounts.tax object and had altered its behaviour as per your business needs.

Eventually if you feed data by calling those methods manually without using Open ERP Web service its possible you'll get undesired result / unexpected failures / inconsistent database state.




回答3:


You can use Erppeek to browse data, but not sure if you can really upload data to DB, personally I use/prefer XMLRPC




回答4:


Why don't you use the xmlrpc call of openerp. it will not need to import account or openerp . and even you can have all orm functionality.




回答5:


You can use python library to access openerp server using xmlrpc service. Please check https://github.com/OpenERP/openerp-client-lib

It is officially supported by OpenERP SA.




回答6:


If you want to interacti directly with the DB, you could just import psycopg2 and:

conn = psycopg2.connect(dbname='dbname', user='dbuser', password='dbpassword', host='dbhost')
cur = conn.cursor()
cur.execute('select * from table where id = %d' % table_id)
cur.execute('insert into table(column1, column2) values(%d, %d)' % (value1, value2))
cur.close()
conn.close()



回答7:


Why you want to fix it like that?! You should create a localization module and define data in XML files. This is the standard way to fix such a problem in OpenERP.

You want to insert sales taxes for which country? Explain more plz.




回答8:


from openerp.modules.registry import RegistryManager
registry = RegistryManager.get("databasename")
with registry.cursor() as cr:
      user = registry.get('res.users').browse(cr, userid, listids)
      print user



来源:https://stackoverflow.com/questions/11493578/how-to-write-a-python-script-that-uses-the-openerp-orm-to-directly-upload-to-pos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!