Odoo 19 continues to empower businesses with its robust, open-source ERP framework, where the structured management of data remains a cornerstone of module development and system configuration. A fundamental aspect of this process involves the use of data files to seamlessly populate and manage information within the database. This blog delves into the mechanics of data files and their core operations in the latest Odoo 19 environment.
The Foundation: XML File Structure
Data integration in Odoo is primarily achieved using XML files, which provide a clear and hierarchical method for defining records. The structure of these files is both simple and powerful, beginning with a root element that encapsulates all data operations.
xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<operation/>
</odoo>
<odoo>: This is the mandatory root element for any Odoo data file. All subsequent operations must be nested within this tag.
<operation>: This is a placeholder representing any action to be performed, such as creating a record (<record>), deleting data (<delete>), or executing a function (<function>). A single file can contain multiple operations.
Controlling Data Load Behavior with the noupdate Attribute
Odoo 19 offers precise control over how data is loaded during module installation and updates through the <data> tag and its noupdate attribute.
xml
<odoo>
<data noupdate="1">
<!-- Records here are loaded ONLY on initial module install -->
<record id="unique_config" model="res.config.settings">
<field name="global_goal">1000</field>
</record>
</data>
<data>
<!-- Records here are loaded on BOTH install and update -->
<record id="product_template_1" model="product.template">
<field name="name">Generic Product</field>
</record>
</data>
</odoo>
<data noupdate="1">: Records enclosed in this tag are created only when the module is first installed (-i module). Subsequent module updates (-u module) will ignore these records. This is ideal for initial configuration data or default records that should not be overwritten.
<data> (without noupdate): Records here are processed every time the module is installed or updated, making them suitable for data that may need to be refreshed or modified.
Core Data Operations
1. The <record> Tag
The <record> tag is the primary method for creating or updating database entries. Its key attributes define what is created and where.
id: Provides a unique external identifier (XML ID) used to reference this record from other parts of the code.
model: Specifies the target Odoo model (e.g., res.partner, product.template) where the record will be created.
Example: Creating a Partner Record
xml
<record id="vendor_corp" model="res.partner">
<field name="name">Vendor Corporation</field>
<field name="email">contact@vendor.com</field>
<field name="company_type">company</field>
</record>
2. The <field> Tag
Within a <record>, the <field> tag is used to assign values to the model's fields. It supports various attributes for different data types and relationships.
name: The name of the field in the model.
ref: Used to link to another record via its external ID, essential for setting up Many2one relations.
type: (Less common) Sometimes used to explicitly define how the field's value should be interpreted (e.g., base64 for binary data, xml/html for structured content).
Examples of Field Usage:
xml
<!-- Simple Char Field -->
<field name="name">New Product</field>
<!-- Many2one Relation using 'ref' -->
<field name="categ_id" ref="product.product_category_all"/>
<!-- Setting a value by evaluating a Python expression -->
<field name="date" eval="(datetime.now() + timedelta(days=7)).strftime('%Y-%m-%d')"/>
<!-- Loading an image from a file path as base64 -->
<field name="image_1920" type="base64" file="my_module/static/img/logo.png"/>
3. The <function> Tag
This powerful tag allows you to call Python methods defined on an Odoo model directly from an XML file. It is typically used for complex setup tasks that cannot be achieved with simple record creation.
Example: Installing a New Language
The following example shows how a function can be called to install a language, with values passed as parameters.
xml
<data noupdate="1"><!-- es_419 is the new "generic" spanish --><record id="base.lang_es" model="res.lang"><field name="url_code">es_ES</field></record><record id="base.lang_es_419" model="res.lang"><field name="url_code">es</field></record><function name="install_lang" model="res.lang"/></data>
In Odoo 19, data files are not just an import mechanism but a sophisticated tool for building a consistent and configurable application state. Mastering the use of XML to define records, manipulate fields, and even execute functions is crucial for developers aiming to create resilient and customizable modules. By leveraging attributes like noupdate and understanding the nuances of different operations, one can ensure a seamless and controlled data integration process, ultimately leading to a more efficient and tailored Odoo implementation.
To read more about An Overview of Data Files Core Operations in Odoo 18, refer to our blog An Overview of Data Files Core Operations in Odoo 18.