odoo import data from associated table

odoo import data from associated table
     Importing data from odoo is very simple, but it is more troublesome to import the data of the associated table if there are foreign keys. Generally, import a table first, then use vlookup in the excel table to find the foreign key, and then import it. If it is imported in the test phase, it needs to be imported in this way when it is officially running, or it needs to be imported multiple times due to debugging problems during the test, which is very troublesome.
  Fortunately, odoo gave a method. I used it once before, but this time I forgot what to do. I read the odoo document, tried it, and found the right one:
  The odoo document is in the import page, and it is pasted here:
    
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","Marketing No.1 Center"
"__export__.hr_department_17","Marketing No.2 Center

 

res_users:
login,name,department_id/id

zhangsan, Zhang San, __export__.hr_department_16
lisi, Li Si, __export__.hr_department_17
                                   

        

         According to odoo's help documentation above, the changes are as follows:

          hr_department:

          "External ID","name"
          "hr_department_6","Marketing 1 Center"
          "hr_department_7","Marketing 2 Center

 res_uers:

           login,name,department_id/External ID

zhangsan, Zhang San, hr_department_6
lisi, Li Si, hr_department_7
                                   

      

 Use the editing tool to replace "__export__.", change the id to   "External ID", and then import it into odoo, the two tables are associated, which is very convenient.

2016.02.14
Remember: You need to re-import the two tables, so that the two sides can correspond. This time, after many attempts, it was a great success. Better to re-import it. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525219&siteId=291194637