CSV Data Serialization

Since version 4.2, OpenERP provides a Comma-Separated-Values (CSV), spreadsheet-compatible data serialization format.

The basic format of an OpenERP CSV file is as follows:

  1. "id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
  2. "access_product_uom_categ_manager","product.uom.categ manager","model_product_uom_categ","product.group_product_manager",1,1,1,1
  3. "access_product_uom_manager","product.uom manager","model_product_uom","product.group_product_manager",1,1,1,1
  4. "access_product_ul_manager","product.ul manager","model_product_ul","product.group_product_manager",1,1,1,1
  5. "access_product_category_manager","product.category manager","model_product_category","product.group_product_manager",1,1,1,1
  6. "access_product_template_manager","product.template manager","model_product_template","product.group_product_manager",1,1,1,1
  7. "access_product_product_manager","product.product manager","model_product_product","product.group_product_manager",1,1,1,1
  8. "access_product_packaging_manager","product.packaging manager","model_product_packaging","product.group_product_manager",1,1,1,1
  9. "access_product_uom_categ_user","product.uom.categ.user","model_product_uom_categ","base.group_user",1,0,0,0
  10. "access_product_uom_user","product.uom.user","model_product_uom","base.group_user",1,0,0,0
  11. "access_product_ul_user","product.ul.user","model_product_ul","base.group_user",1,0,0,0
  12. "access_product_category_user","product.category.user","model_product_category","base.group_user",1,0,0,0
  13. "access_product_template_user","product.template.user","model_product_template","base.group_user",1,0,0,0
  14. "access_product_product_user","product.product.user","model_product_product","base.group_user",1,0,0,0
  15. "access_product_packaging_user","product.packaging.user","model_product_packaging","base.group_user",1,0,0,0

Importing from a CSV

Instead of using .XML file, you can import .CSV files. It is simpler but the migration system does not migrate the data imported from the .CSV files. It’s like the noupdate attribute in .XML files. It’s also more difficult to keep track of relations between resources and it is slower at the installation of the server.

Use this only for [demo] data that will never been upgraded from one version of OpenERP to another.

The name of the object is the name of the CSV file before the first ‘-‘. You must use one file per object to import. For example, to import a file with partners (including their multiple contacts and events), the file must be named like one of the following example:

  • res.partner.csv

  • res.partner-tiny_demo.csv

  • res.partner-tiny.demo.csv

Structure of the CSV file

  • Separator to use: ,

  • Quote character for strings: “ (optional if no separator is found in field values)

  • Encoding to use: UTF-8

  • No whitespace allowed around separators if not using quote characters

  • Be sure to configure your CSV export software (e.g. spreadsheet editor) with the above parameters

Exporting demo data and import it from a module

You can import .CSV file that have been exported from the OpenERP client. This is interesting to create your own demo module. But both formats are not exactly the same, mainly due to the conversion: Structured Data -> Flat Data -> Structured Data.

  • The name of the column (first line of the .CSV file) use the end user term in his own language when you export from the client. If you want to import from a module, you must convert the first column using the fields names. Example, from the partner form:

    1. Name,Code,Contacts/Contact Name,Contacts/Street,Contacts/Zip

    becomes:

    1. name,ref,address/name,address/street,address/zip
  • When you export from the OpenERP client, you can select any many2one fields and their child’s relation. When you import from a module, OpenERP tries to recreate the relations between the two resources. For example, do not export something like this from a sale order form - otherwise OpenERP will not be able to import your file:

    1. Order Description,Partner/Name,Partner/Payable,Partner/Address/Name
  • To find the link for a many2one or many2many field, the server uses the name_search function when importing. So, for a many2one field, it is better to export the field ‘name’ or ‘code’ of the related resource only. Use the more unique one. Be sure that the field you export is searchable by the name_search function. (the ‘name’ column is always searchable):

    1. Order Description,Partner/Code
  • Change the title of the column for all many2many or many2one fields. It’s because you export the related resource and you import a link on the resource. Example from a sale order: Partner/Code should become partner_id and not partner_id/code. If you kept the /code, OpenERP will try to create those entries in the database instead of finding references to existing ones.

  • Many2many fields. If all the exported data contains 0 or 1 relation on each many2many fields, there will be no problem. Otherwise, the export will result in one line per many2many. The import function expects to get all many2many relations in one column, separated by a comma.

    So, you have to make the transformation. For example, if the categories “Customer” and “Supplier” already exists:

    1. name,category_id
    2. Smith, "Customer, Supplier"

    If you want to create these two categories you can try

    1. name,category_id/name
    2. Smith, "Customer, Supplier"

    But this does not work as expected: a category “Customer, Supplier” is created. The solution is to create an empty line with only the second category:

    1. name,category_id/name
    2. Smith, Customer
    3. ,Supplier

    Note the comma before “Supplier”!

  • Read only fields. Do not try to import read only fields like the amount receivable or payable for a partner. Otherwise, OpenERP will not accept to import your file.

  • Exporting trees. You can export and import tree structures using the parent field. You just have to take care of the import order. The parent have to be created before his child’s.

Use record id like in xml file:

It’s possible to define an id for each line of the csv file. This allow to define references between records:

id, name, parent_id:id record_one, Father, record_two, Child, record_one

If you do this, the line with the parent data must be before the child lines in the file.