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
記住: 要把兩表重新導入,這樣兩邊才能對應起來,這次試了好多 次,才大成功。還是重新導入一次比較好。 

發佈了35 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章