Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท Modules and Business Apps

Data Files and XML IDs

Load records with Odoo XML and CSV data files: record, field, ref, eval, external ids, noupdate and how updates treat existing records.

Python defines what can exist in Odoo. Data files define what does exist from the start: menus, views, groups, email templates, sequences, default categories, demo customers. They are XML (or CSV) files listed in the manifest and loaded when the module is installed or updated.

You already saw record tags in the first module. This lesson explains the full toolbox, in particular external ids, ref, eval and noupdate, which cause most data-file puzzles.

The record tag

XML
<odoo>
  <record id="category_small_pets" model="product.category">
    <field name="name">Small pets</field>
    <field name="parent_id" ref="product.product_category_all"/>
  </record>
</odoo>
  • id is the record's local XML id. After loading it is known as module.category_small_pets, and from other modules you must write it with the module prefix.
  • model is the model the record belongs to.
  • Each field tag sets a value. Plain text becomes a string (or is converted to the field's type, for example numbers and booleans).
  • ref="module.name" points to another record through its external id. The loader looks the id up in ir_model_data and stores the numeric id.

Inside the same module you may omit the module prefix: ref="category_small_pets".

eval and special values

When the value is code rather than text, use eval:

XML
<record id="demo_pet_rex" model="vet.pet">
  <field name="name">Rex</field>
  <field name="weight" eval="31.5"/>
  <field name="active" eval="True"/>
  <field name="owner_id" ref="base.res_partner_1"/>
  <field name="category_ids" eval="[(6, 0, [ref('category_small_pets')])]"/>
</record>

eval runs a restricted Python expression. Inside it, ref('xml_id') returns the numeric id, which is how you fill relational fields. The list [(6, 0, [ids])] is a command tuple that replaces all links of a many-to-many field (chapter 4 explains these commands; in newer series you can also use the Command helper).

Updating, creating and deleting

The same file is processed on install and on every update. The loader uses the XML id to decide:

  • id does not exist yet in ir_model_data: create a new record.
  • id exists: update that record with the values in the file (unless noupdate).

To remove a record that an earlier version of your module created, use the delete tag:

XML
<delete model="ir.ui.view" id="old_pet_view"/>

noupdate

Sometimes you want data to be created once and then never overwritten, because users are expected to edit it: email templates, sequences, default settings. Wrap such records in noupdate="1":

XML
<odoo noupdate="1">
  <record id="seq_pet_code" model="ir.sequence">
    <field name="name">Pet code</field>
    <field name="code">vet.pet</field>
    <field name="prefix">PET/</field>
    <field name="padding">5</field>
  </record>
</odoo>

On the first install the record is created. On later updates it is left alone, so the user's edits survive. Do not put views or security rules in noupdate blocks; those must be updated when your code changes.

Functions, CSV and other file types

  • CSV data: a file named after the model, for example security/ir.model.access.csv or data/res.partner.csv, with a header row of field names. For relational fields use field_id:id columns to give external ids. This format is compact for large lists of simple records.
  • Demo data: the same XML syntax, listed under demo in the manifest.
  • function tag: calls a model method at load time, for rare cases where creation needs code. Prefer plain records.
  • Post-init hooks in Python can do one-time setup that XML cannot express (see the module lifecycle lesson).

External ids in practice

Because external ids are stable names, you will use them everywhere:

  • in XML, as ref="sale.view_order_form",
  • in Python, with self.env.ref("base.main_company"),
  • in the browser, in menu and action definitions,
  • in the debug menu, "View Metadata" shows a record's XML id.

If a name does not resolve, the load fails with an error such as "External ID not found in the system". The usual reasons are a missing module prefix, a dependency not declared, or the file with the definition loaded later in the manifest.

flowchart LR
    X["views.xml<br/>record id='vet_pet_action'"] -->|"load"| D["ir_model_data:<br/>vet_clinic.vet_pet_action -> ir.actions.act_window #77"]
    M["menus.xml<br/>action='vet_pet_action'"] -->|"lookup"| D
    D --> R["numeric id 77"]

The loader translates readable names into numeric ids, so the same files work on every database.

A runnable stand-in: a mini data loader

The following stand-in parses an XML snippet with Python's standard library and loads it into an in-memory store, resolving ref, honouring noupdate and reporting create versus update. Odoo's real loader is far richer, but the logic for ids is the same.

Python Stand-in: loading XML records with ids

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it and compare the first two load calls: the second finds the existing ids and updates. The last call shows noupdate protecting a record.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. From another module, how do you refer to the record with local id vet_pet_action defined in vet_clinic?

    Outside the defining module the external id needs the module prefix. Within the same module the prefix is optional.
  2. 2. What does noupdate="1" do?

    It protects records users are expected to edit, such as email templates and sequences.
  3. 3. Which attribute fills a relational field with a record defined elsewhere?

    ref looks the external id up and stores the numeric id.
  4. 4. Why should views not be placed inside a noupdate block?

    Views, actions and security rules are part of your code and must follow your updates.

Summary

  • Data files create records from XML (record, field, delete) or CSV.
  • Every record gets an external id module.name; ref and ref() turn it into a numeric id.
  • Updating a module re-reads its data files, updating existing ids and creating new ones.
  • noupdate="1" keeps one-time defaults such as sequences and templates from being overwritten.
  • eval evaluates a restricted expression for numbers, booleans and relational commands.

Next: The Standard Business Apps