使用cursor.commit和psycopg2插入具有特定id的Odoo数据库

时间:2016-10-26 10:50:48

标签: python postgresql openerp psycopg2

我试图通过代码将一些模型从OpenERP 7迁移到Odoo 8。我想将对象插入到保持原始id号的新表中,但它并没有这样做。

我想插入新对象,包括其ID号。

我的代码:

import openerp
from openerp import api, modules
from openerp.cli import Command

import psycopg2

class ImportCategory(Command):
    """Import categories from 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 to the 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 to retrieve source model data
            src_cr.execute("""
                SELECT c.id, c.parent_id, c.name, c.type 
                    FROM product_category c
                    ORDER BY c.id;
            """)
        except psycopg2.Error as 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()

        with 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()

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我能找到的唯一方法是在其他对象中使用引用属性在新数据库中将它们关联起来。我的意思是创建关于位置代码,客户端代码,订单号的关系......当它们在目标数据库中创建时,查找它们并使用新ID。

def run(self, cmdargs):
    # Connection to the 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 to 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
                FROM res_company c, account_fiscalyear fy, account_period p
                WHERE p.fiscalyear_id = fy.id AND c.id = fy.company_id AND p.company_id = fy.company_id
                ORDER BY fy.id;
        """)

    except psycopg2.Error as 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()

    with 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)

以前的代码只是整个源代码的摘录。