6
votes

I can import Comma Separated Values (CSV) data through the admin pages, into most models. This process handles the external IDs so that the data can be added to or amended as appropriate in later CSV imports. This is a manial action.

Through the API, the same records can be created and amended, and external IDs can be set. This, however, requires a lot of the logic that would otherwise be handled by the CSV importer to be coded by hand, in the external application that uses the API to push in data. Pushing data through the API can be automated.

Is there a way the API can be used (so no changes need to be made to code within Odoo) to push CSV data (so the logic for insert/update/relationships/external IDs/ etc. is handled by Odoo)? This would be a kind of hybrid approach, and I am trying to avoid the need to create import modules within Odoo.

Edit: the "external ID" is often called the "XML ID". I think it is a terminology that has stuck from earlier versions of OpenERP, rather than having anything specific to do with XML.

Edit

This page describes a load() function that pushes CSV-like data through a pipeline to load it into the system:

http://openerp-server.readthedocs.org/en/latest/06_misc_import.html

I can't see how to translate the summary on that page into an operation through the API, if indeed that is possible. I'm guessing I will need the interface (entry point), model, method (load(), probably), and some additional parameters, but the details are beyond me.

2
It would be great to be able to export CSV through the API too. Fetching data direct from the models is a many-step process in order to get hold of external IDs in the main model records and all their lookup values (e.g. a list of states and the countries they are in - the countries are returned by internal database ID only, and the external ID needs to be looked up separately.Jason

2 Answers

4
votes

The answer is kind of "yes".

The load() method can be used against any model to load data. This method takes data in the same structure as a CSV file would provide.

  • The first parameter is an array of field names, like the column headings on a CSV import.
  • The second parameter is an array of records. Each record is an array of values matching each field.

The API will return a list of errors where they are catered for by OpenERP. Many errors, however, just result in database exceptions on OpenERP and so need to be picked up as an API failure. This is largely because the OpenERP API is not designed as a generic API, but as a part of the GUI, and so the data sent to the API is very much bound to the current state of the application through that GUI. In other words, invalid data will seldom find its way to the API using the OpenERP GUI.

I have wrapped the loader functionality, catching errors and exceptions, in my PHP OpenERP API library here:

https://github.com/academe/openerpapi/blob/master/src/App/Loader.php

Hopefully that will be useful to others too.

0
votes

I think the answer is "no".

However, this technique has been explained to me:

  1. Create a module with little in it but CSV files for importing.
  2. Install the module.
  3. When a new CSV file needs to be imported, transfer it into the module (FTP or similar).
  4. Once transferred, run the update() method for the module. This can be done through the API.

The update method will scan and load all the CSV files set up within the module. Care needs to be taken to make sure only one upload/update transaction will be run at any time.

I'll post additional details here when I have got this working, or will happily accept an alternate answer if there is a better way to handle this.