Loading data using CSV files

While you can do everything you need with XML files, this format is not the most convenient when you need to provide larger amounts of data, especially given that many people are more comfortable preprocessing data in Calc, or some other spreadsheet software. Another advantage of this format is that it is what you get when you use the standard export function. In this recipe, we'll have a look at importing table-like data.

How to do it...

Traditionally, access control lists (ACLs), (refer to Chapter 10, Access Security) are a type of data that is loaded via CSV files:

  1. Add security/ir.model.access.csv to your data files:
    'data': [
        …
        'security/ir.model.access.csv',
    ],
  2. Add an ACL for our books in this file:
    "id","name","model_id:id","group_id:id","perm_read",
        "perm_write","perm_create","perm_unlink"
    "access_library_book_user","ACL for books",
        "model_library_book","base.group_user",1,0,0,0

Now we have an ACL that permits normal users to read book records, but does not allow them to edit, create or delete them.

How it works...

You simply drop all your data files in your manifest's data list. Odoo will use the file extension to decide which type of file it is. A specialty of CSV files is that their file name must match the name of the model to be imported, in our case, ir.model.access. The first line needs to be a header with column names that match the model's field names exactly.

For scalar values, you can use a quoted (if necessary because the string contains quotes or commas itself) or an unquoted string.

When writing many2one fields with a CSV file, Odoo first tries to interpret the column value as an XML ID: If there's no dot, add the current module name as a namespace, lookup the result in ir.model.data. In case this fails, the model's name_search function is called with the column's value as a parameter and the first result returned wins. When this also fails, the line is considered invalid and Odoo raises an error.

Note

Note that data read from CSV files is always noupdate=False and there's no convenient way around this. This means subsequent updates of your addon will always overwrite possible changes made by the user.

There's more...

Importing one2many and many2many fields with CSV files is possible, but a bit tricky. In general, you're better off creating the records separately and setting up the relation with an XML file afterwards, or working with a second CSV file that sets up the relationship.

If you really need to create related records within the same file, order your columns so that all scalar fields are to the left and fields of the linked model are to the right, with a column header consisting of the linking field's name and the linked model's field, separated by a colon:

"id","name","model_id:id","perm_read","perm_read", "group_id:name",
"access_library_book_user","ACL for books","model_library_book",1,"my group"

This would create a group called my group, you could write more fields in the group record by adding columns to the right. If you need to link multiple records, repeat the line and only change the right hand columns as appropriate. Given that Odoo fills empty columns with the previous line's value, you don't need to copy all data, but simply add a line with empty values saved for the fields of the linked model you want to fill.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.157