We will start exploring how data exporting and importing work from Odoo's user interface, and from there, we will move on to the more technical details on how to use the data files in our addon modules.
Data exporting is a standard feature available in any list view. To use it, we must first pick the rows to export by selecting the corresponding checkboxes on the far left and then select the Export option from the More button.
Here is an example, using the recently created to-do tasks:
We can also tick the checkbox in the header of the column. It will check all the records at once, and will export all the records that match the current search criteria.
In previous Odoo versions, only the records seen on the screen (the current page) would actually be exported. Since Odoo 9, this was changed and ticked checkbox in the header will export all records that match the current filter, not just the ones currently displayed. This is very useful to export large sets of records that do not fit on the screen.
The Export option takes us to a dialog form, where we can choose what to export. The Import-Compatible Export option makes sure that the exported file can be imported back to Odoo. We will need to use this.
The export format can be either CSV or Excel. We will prefer a CSV file to get a better understanding of the export format. Next, we pick the columns we want to export and click on the Export To File button. This will start the download of a file with the exported data:
If we follow these instructions and select the fields shown in the preceding screenshot, we should end up with a CSV text file similar to this:
"id","name","user_id/id","date_deadline","is_done" "todo_user.todo_task_a","Install Odoo","base.user_root","2015-01-30","False" "__export__.todo_task_9","Create my first module","base.user_root","","False"
Notice that Odoo automatically exported an additional id
column. This is an external identifier assigned to each record. If none is already assigned, a new one is automatically generated using __export__
in place of an actual module name. New identifiers are only assigned to records that don't already have one, and from there on, they are kept bound to the same record. This means that subsequent exports will preserve the same external identifiers.
First we have to make sure the import feature is enabled. Since Odoo 9 it is enabled by default. If not, the option is available from the Settings top menu, General Settings option. Under the Import | Export section there is a Allow users to import data from CSV/XLS/XLSX/ODS files checkbox that should be enabled.
With this option enabled list views show an Import option next to the Create button at the top of the list.
Let's perform a bulk edit on our to-do data first. Open the CSV file we just downloaded in a spreadsheet or a text editor and change a few values. Also, add some new rows, leaving the id
column blank.
As mentioned before, the first column, id
, provides a unique identifier for each row. This allows already existing records to be updated instead of duplicating them when we import the data back to Odoo. For new rows added to the CSV file, we can choose to either provide an external identifier of our choice, or to leave blank the id
column, a new record will be created for them.
After saving the changes to the CSV file, click on the Import option (next to the Create button) and we will be presented with the import assistant.
There, we should select the CSV file location on the disk and click on Validate to check its format for correctness. Since the file to import is based on an Odoo export, chances are that it will be valid:
Now we can click on Import, and there you go; our modifications and new records should have been loaded into Odoo.
In the preceding example, the user responsible for each task is a related record in the users model, with a many-to-one (or a foreign key) relation. The column name used for it was user_id/id
and the field values were external identifiers for the related records, such as base.user_root
for the administrator user.
Relation columns should have /id
appended to their name if using external identifiers or /.id
if using database (numeric) IDs. Alternatively, a colon (:
) can be used in place of a slash for the same effect.
Similarly, many-to-many relations are also supported. An example of a many-to-many relation is the one between users and groups: each user can be in many groups, and each group can have many users. The column name for this type of field should have /id
appended. The field values accept a comma-separated list of external identifiers, surrounded by double quotes.
For example, the to-do task followers have a many-to-many relation between to-do tasks and partners. Its column name should be follower_ids/id
, and a field value with two followers could look like this:
"__export__.res_partner_1,__export__.res_partner_2"
Finally, one-to-many relations can also be imported through a CSV. The typical example for this type of relation is a document head with several lines. Notice that a one-to-many relation is always the inverse of a many-to-one relations. Each document head can have many lines. And inversely every line has one head.
We can see an example of such a relation in the company model (the form view is available in the Settings menu): each company can have several bank accounts, each with its own details; inversely, each bank account record belongs to and has a many-to-one relation with only one company.
We can import companies along with their bank accounts in a single file. Here is an example where we load a company with three banks:
id,name,bank_ids/id,bank_ids/acc_number,bank_ids/state base.main_company,YourCompany,__export__.res_partner_bank_4,123456789,bank ,,__export__.res_partner_bank_5,135792468,bank ,,__export__.res_partner_bank_6,1122334455,bank
We can see that the first two columns, id
and name
, have values in the first line and are empty in the next two lines. They have data for the head
record, which is the company.
The other three columns are all prefixed with bank_ids/
and have values on all three lines. They have data for the three related lines for the company's bank accounts. The first line has data of both the company and the first bank, and the next two lines have data only for the additional company and banks.
These are essentials while working with export and import from the GUI. It is useful to set up data in new Odoo instances or to prepare data files to be included in Odoo modules. Next, we will learn more about using data files in modules.