odoo导入关联表的数据

odoo导入关联表的数据
     odoo导入数据很简单,但导入关联表的数据,有外键的就麻烦一些,一般先导入一张表,然后用excel表用vlookup,找到外键,然后导入,如果一次性导入也还可以,若是测试阶段导入,正式运行时,还要这样导入,或测试时因调试出问题,需要多次导入,就很麻烦了。
  好在odoo给出了方法,以前用过一次,这次竟忘记该怎么做了,看odoo文档,试了试,发现对头:
  odoo文档就在导入页里,粘在这里:
    
How to export/import different tables from an SQL application to Odoo?

If you need to import data from different tables, you will have to recreate relations between records belonging to different tables. (e.g. if you import companies and persons, you will have to recreate the link between each person and the company they work for).

To manage relations between tables, you can use the "External ID" facilities of Odoo. The "External ID" of a record is the unique identifier of this record in another application. This "External ID" must be unique accoss all the records of all objects, so it's a good practice to prefix this "External ID" with the name of the application or table. (like 'company_1', 'person_1' instead of '1')

As an example, suppose you have a SQL database with two tables you want to import: companies and persons. Each person belong to one company, so you will have to recreate the link between a person and the company he work for. (If you want to test this example, here is a dump of such a PostgreSQL database).

We will first export all companies and their "External ID". In PSQL, write the following command:

    copy (select 'company_'||id as "External ID",company_name as "Name",'True' as "Is a Company" from companies) TO '/tmp/company.csv' with CSV HEADER;

This SQL command will create the following CSV file:
    External ID,Name,Is a Company
    company_1,Bigees,True
    company_2,Organi,True
    company_3,Boum,True

To create the CSV file for persons, linked to companies, we will use the following SQL command in PSQL:

    copy (select 'person_'||id as "External ID",person_name as "Name",'False' as "Is a Company",'company_'||company_id as "Related Company/External ID" from persons) TO '/tmp/person.csv' with CSV

It will produce the following CSV file:
    External ID,Name,Is a Company,Related Company/External ID
    person_1,Fabien,False,company_1
    person_2,Laurence,False,company_1
    person_3,Eric,False,company_2
    person_4,Ramsy,False,company_3

As you can see in this file, Fabien and Laurence are working for the Bigees company (company_1) and Eric is working for the Organi company. The relation between persons and companies is done using the External ID of the companies. We had to prefix the "External ID" by the name of the table to avoid a conflict of ID between persons and companies (person_1 and company_1 who shared the same ID 1 in the orignial database).

The two files produced are ready to be imported in Odoo without any modifications. After having imported these two CSV files, you will have 4 contacts and 3 companies. (the firsts two contacts are linked to the first company). You must first import the companies and then the persons.

例子:
先导出两个关联表的数据如下:

部门表:hr_department:

"id","name"
"__export__.hr_department_16","营销一中心"
"__export__.hr_department_17","营销二中心

 

res_users:
login,name,department_id/id

zhangsan,张三,__export__.hr_department_16
lisi,李四,__export__.hr_department_17
                                   

        

         根据上面odoo的帮助文档,更改如下:

          hr_department:

          "External ID","name"
          "hr_department_6","营销一中心"
          "hr_department_7","营销二中心

 res_uers:

           login,name,department_id/External ID

zhangsan,张三,hr_department_6
lisi,李四,hr_department_7
                                   

      

 用编辑工具,替换掉"__export__.”,把id 改成   "External ID",再往odoo里导入, 两个表就关联起来了,很方便。

2016.02.14
记住: 要把两表重新导入,这样两边才能对应起来,这次试了好多 次,才大成功。还是重新导入一次比较好。 

猜你喜欢

转载自blog.csdn.net/xtjie/article/details/76082970