posted on December 21, 2012 18:32
In the previous article and video we demonstrated how to import data into OpenERP using the XML-RPC method. There are some advantages of using XML-PRC method of importing data. It works through the ORM (Object Relationship Model) and has better integrity protection of your data. Unfortunately going through XML-RPC is very slow. Even on very fast computers each record insert can take as much as a full second. This is not a big deal if you are only importing a few dozen records. If however you are importing thousands of records or perhaps millions of records the XML-RPC method is simply too slow to be an adequate solution.
In the video below we will import data into OpenERP by inserting directly into the PostGRES database. The performance is well over 100 times faster. The downside is that you will have to be more responsible for maintaining the data integrity in your script. So make sure you test well and if you are ever running an import script on a production system make sure you backup everything. You will also want to turn off the OpenERP server (leave the PostGRES server running of course) just to make sure you don't have any unforeseen issues.
The script used in the video is provided below.
OpenERP 7 - Example Python Script for importing data directly into PostGRES database
conn_string = "dbname='db2' user='openpg' password='openpgpwd'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
reader = csv.reader(open('products_import.csv','rb'))
for row in reader:
statement = "INSERT INTO product_template (name,standard_price,list_price,mes_type,uom_id,uom_po_id," \
"type,procure_method,cost_method,categ_id,supply_method,sale_ok) VALUES ('" + row + "'," \
+ str(row) + "," + str(row) + ",'fixed',1,1,'product','make_to_stock','standard',1,'buy',True) RETURNING id"
templateid = cursor.fetchone()
statement = "INSERT INTO product_product (product_tmpl_id,default_code,active,valuation) VALUES \
(" + str(templateid) + ",'" + row + "',True,'manual_periodic')"