While CSV files provide a simple and compact format to represent data, XML files are more powerful and give more control over the loading process. Their filenames are not required to match the model to be loaded. This is because the XML format is much richer and that information is provided by the XML elements inside the file.

We already used XML data files in the previous chapters. The user interface components, such as views and menu items, are in fact records stored in system models. The XML files in the modules are the means used to load these records into the server.

To showcase this, we will add a second data file to the todo_user module, data/todo_data.xml, with the following content:

<?xml version="1.0"?> 
<odoo> 
  <!-- Data to load --> 
  <record model="todo.task" id="todo_task_c"> 
    <field name="name">Reinstall Odoo</field> 
    <field name="user_id" ref="base.user_root" /> 
    <field name="date_deadline">2015-01-30</field> 
    <field name="is_done" eval="False" /> 
  </record> 
</odoo> 

This XML is equivalent to the CSV data file we just saw in the previous section.

XML data files have a <odoo> top element, inside of which we can have several <record> elements that correspond to the CSV data rows.

A <record> element has two mandatory attributes, namely model and id (the external identifier for the record), and contains a <field> tag for each field to write on.

Note that the slash notation in field names is not available here; we can't use <field name="user_id/id">. Instead, the ref special attribute is used to reference external identifiers. We'll discuss the values of the relational to-many fields in a moment.

When data loading is repeated, records loaded from the previous run are rewritten. It is important to keep in mind that this means that upgrading a module will overwrite any manual changes that might have been made inside the database. Notably, if views were modified with customizations, then these changes will be lost with the next module upgrade. The correct procedure is to instead create inherited views for the changes we need, as discussed in Chapter 3 , Inheritance - Extending Existing Applications.

This re-import behavior is default, but it can be changed, so that when an module is upgraded, some data file records are left untouched. This is done by the noupdate="1" attribute of the <odoo> or <data> element. These records will be created when the addon module is installed, but in subsequent module upgrades nothing will be done to them.

This allows you to ensure that manually made customizations are kept safe from module upgrades. It is often used with record access rules, allowing them to be adapted to implementation-specific needs.

It is possible to have more than one <data> section in the same XML file. We can take advantage of this to separate data to import only one,  with noupdate="1", and data to be re-imported on each upgrade, with noupdate="0".

The noupdate flag is stored in the External Identifier information for each record. It's possible to manually edit it directly using the External Identifier form available in the Technical menu, using the Non Updatable checkbox.

Each <record> element has two basic attributes, id and model, and contains <field> elements that assign values to each column. As mentioned before, the id attribute corresponds to the record's external identifier and the model attribute to the target model where the record will be written. The <field> elements have a few different ways to assign values. Let's look at them in detail.

If we go back to Chapter 2 , Building Your First Odoo Application, we will find elements other than <record>, such as <act_window> and <menuitem>, in the XML files.

These are convenient shortcuts for frequently used models that can also be loaded using regular <record> elements. They load data into base models supporting the user interface and will be explored in more detail later, specifically in Chapter 6 , Views - Designing the User Interface.

For reference, the following shortcut elements are available with the corresponding models they load data into:

  • <act_window> is the window action model, ir.actions.act_window
  • <menuitem> is the menu items model, ir.ui.menu
  • <report> is the report action model, ir.actions.report.xml
  • <template> is for QWeb templates stored in the model ir.ui.view
  • <url> is the URL action model, ir.actions.act_url

Until now, we have seen how to add or update data using XML files. But XML files also allow you to perform other types of actions that are sometimes needed to set up data. In particular, they can delete data, execute arbitrary model methods, and trigger workflow events.