python - Insert into Odoo db with a specific id using cursor.commit and psycopg2 -
i'm trying migrate models openerp 7 odoo 8 code. want insert objects new table maintaining original id number, doesn't it.
i want insert new object including id number.
my code:
import openerp openerp import api, modules openerp.cli import command import psycopg2 class importcategory(command): """import categories source db""" def process_item(self, model, data): if not data: return # model structure model.create({ 'id': data['id'], 'parent_id': none, 'type': data['type'], 'name': data['name'] }) def run(self, cmdargs): # connection source database src_db = psycopg2.connect( host="127.0.0.1", port="5432", database="db_name", user="db_user", password="db_password") src_cr = src_db.cursor() try: # query retrieve source model data src_cr.execute(""" select c.id, c.parent_id, c.name, c.type product_category c order c.id; """) except psycopg2.error e: print e.pgerror openerp.tools.config.parse_config(cmdargs) dbname = openerp.tools.config['db_name'] r = modules.registry.registrymanager.get(dbname) cr = r.cursor() api.environment.manage(): env = api.environment(cr, 1, {}) # define target model product_category = env['product.category'] id_ptr = none c_data = {} while true: r = src_cr.fetchone() if not r: self.process_item(product_category, c_data) break if id_ptr != r[0]: self.process_item(product_category, c_data) id_ptr = r[0] c_data = { 'id': r[0], 'parent_id': r[1], 'name': r[2], 'type': r[3] } cr.commit()
how do that?
the way find use reference attributes in others objects relate them in new database. mean create relations on location code, client code, order number... , when created in target database, them , use new id.
def run(self, cmdargs): # connection source database src_db = psycopg2.connect( host="localhost", port="5433", database="bitnami_openerp", user="bn_openerp", password="bffbcc4a") src_cr = src_db.cursor() try: # query retrieve source model data src_cr.execute(""" select fy.id, fy.company_id, fy.create_date, fy.name, p.id, p.code, p.company_id, p.create_date, p.date_start, p.date_stop, p.special, p.state, c.id, c.name res_company c, account_fiscalyear fy, account_period p p.fiscalyear_id = fy.id , c.id = fy.company_id , p.company_id = fy.company_id order fy.id; """) except psycopg2.error e: print e.pgerror openerp.tools.config.parse_config(cmdargs) dbname = openerp.tools.config['db_name'] r = modules.registry.registrymanager.get(dbname) cr = r.cursor() api.environment.manage(): env = api.environment(cr, 1, {}) # define target model account_fiscalyear = env['account.fiscalyear'] id_fy_ptr = none fy_data = {} res_company = env['res.company'] r = src_cr.fetchone() if not r: self.process_fiscalyear(account_fiscalyear, fy_data) break company = res_company.search([('name','like',r[13])]) print "company id: {} | company name: {}".format(company.id,company.name)
the previous code extract whole source code.
Comments
Post a Comment