Home > Configuration, Debian, ETL, Python > Importing products with web-services in OpenERP

Importing products with web-services in OpenERP

Python logo

Image via Wikipedia

Yesterday someone at the OpenERP forum asked me for an example of how to import product data into OpenERP, so here it is, below you will find an example of a Python script for loading basic product data.

#!/usr/bin/env python
# coding: utf-8

import xmlrpclib
import csv

username = ‘admin’ #the user
pwd = ‘pwd_admin’ #the password of the user
dbname = ‘blog_db’ #the database

# Get the uid
sock_common = xmlrpclib.ServerProxy (‘http://localhost:8069/xmlrpc/common’)
uid = sock_common.login(dbname, username, pwd)

#replace localhost with the address of the server
sock = xmlrpclib.ServerProxy(‘http://localhost:8069/xmlrpc/object’)

#load categories first
filename = “categories.csv”
reader = csv.reader(open(filename,”rb”))
for row in reader:
category = {
‘name’: row[1],
‘active’: True,
}
category_id = sock.execute(dbname, uid, pwd, ‘res.partner.category’, ‘create’, category)
print category_id

print “End categories load”

filename = “product_template.csv”
reader = csv.reader(open(filename,”rb”))
for row in reader:
product_template = {
‘name’: row[0].rstrip(),
‘supply_method’:’produce’,
‘standard_price’:1,
‘mes_type’:’fixed’,
‘uom_id’:1,
‘uom_po_id’:1,
‘type’:’product’,
‘procure_method’:’make_to_stock’,
‘cost_method’:’standard’,
‘categ_id’:1}
template_id = sock.execute(dbname, uid, pwd, ‘product.template’, ‘create’, product_template)
print template_id

product_product = {
‘product_tmpl_id’:template_id,
‘default_code’: row[0].rstrip(),
‘active’: True,
}
product_id = sock.execute(dbname,uid,pwd,’product.product’,’create’,product_product)
print product_product

print “End product load”

This is a simple script you can execute from the command line. In summary, first you need to create the categories and then you need to create the product.template objects and product.product objects. If you have any question about this, please let me know.

Cheers!

  1. langton
    May 12, 2011 at 8:55 am

    hi thanx for the code but this code will copy the name to the default code and name fild in the product_product table is there a way of including the actual product code, also can you send me a sample category.csv and product.template.csv files .Regards

  1. No trackbacks yet.

Leave a comment