Odoo, a strong open-source ERP platform, uses data files as an important part of organizing and handling information in the system. In this blog, we will explore data files and operations in Odoo 18.
The core of module creation in Odoo lies in defining the records that the module will oversee. Data can be easily added to Odoo through XML and CSV files. Let's delve into how we can precisely define data in Odoo by leveraging XML files.
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<operation/>
</odoo>
The above example illustrates the root element of the data file. Numerous <operation> elements can be utilized within the root <odoo> element.
<odoo>: The root element of the XML file. Every data file for Odoo must start with this tag.
<operation>: This tag represents an operation to be performed, such as creating or updating records. You can replace it with actual data operations like <record>, <delete>, etc.
<odoo>
<data noupdate="1">
<!-- Only loaded when installing the module (odoo-bin -i module) -->
<operation/>
</data>
<!-- (Re)Loaded at install and update (odoo-bin -i/-u) -->
<operation/>
</odoo>
<data>: This tag encapsulates the records to be created or updated in the database. It can have attributes such as noupdate.
noupdate="1": The handling of the noupdate attribute has been optimized. This attribute indicates that the records inside this <data> tag should only be loaded when the module is installed for the first time. If the module is later updated, the records inside this tag will not be reloaded. This is useful for data that should remain unchanged after the initial installation (e.g., default settings, reference data).
Core Operations
1. Record
<record>
...........
</record>
The <record> tag is used to define or modify database records in Odoo. Here are some commonly used attributes, along with examples:
* id: This attribute provides a unique identifier for the record, which is essential for referencing it in the system.
<record id="partner_1" model="res.partner">
<field name="name">John Doe</field>
</record>
* model: The model attribute specifies the Odoo model or database table linked to the record. By including this attribute, you create a connection between the record and its associated model, allowing the system to manage the record according to the established structure and functionality defined within that model.
<record id="customer_1" model="res.partner">
<field name="name">Jane Smith</field>
<field name="email">jane.smith@example.com</field>
</record>
* Context: You can specify context in action definitions to modify how data is presented when an action is triggered.
<record id="action_your_model" model="ir.actions.act_window">
<field name="name">Your Model</field>
<field name="res_model">your.model</field>
<field name="view_mode">tree,form</field>
<field name="context">{'search_default_filter': 1}</field>
</record>
2. Field
* name: Specifies the field's name, which is required for field definitions.
<field name="partner_id"/>
This example sets the value of the field partner_id for a record.
* ref: Used to reference another record by its external ID, establishing relationships between records.
<field name="category_id" ref="module_name.category_id_example"/>
In this example, category_id is set to refer to a record in another model identified by the external ID module_name.category_id_example.
* type: Specifies the type of data the field will hold, influencing how the field's content is processed and displayed. Odoo provides various built-in types, such as char, text, integer, many2one, one2many, many2many, etc.
<field name="price" type="int"/>
In this case, price is defined as an Integer number.
* Base64: Used to encode and decode binary data, often used for file attachments.
* int: Stores numeric values as integers, without decimal points.
* XML and HTML: These types allow you to store structured documents. You can include external IDs in the format %(external_id)s, and to include literal percent signs, you use %%.
* File: Interprets the field's content as a file path, managing file attachments.
* Char: Represents string data, typically used for shorter text inputs.
* Eval: The eval attribute allows for dynamic computations or evaluations of field values or expressions. It enables the execution of Python code to generate or manipulate values of existing fields.
3. Functions
It can be utilized to invoke a method on a model with specified parameters. It has two mandatory parameters.
* <function name="install_lang" model="res.lang">: This line calls the install_lang method on the res.lang model.
* The <value> tag within it specifies a parameter being passed to the method; in this case, the language code 'mya'
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<function name="install_lang" model="res.lang"/>
</data>
<data>
<!-- /my is for the portal -->
<record id="base.lang_my" model="res.lang">
<field name="url_code">mya</field>
</record>
</data>
<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>
</data>
<data>
<record id="base.lang_ar" model="res.lang">
<field name="flag_image" type="base64" file="base/static/img/lang_flags/lang_ar.png"/>
</record>
<record id="base.lang_sr@latin" model="res.lang">
<field name="flag_image" type="base64" file="base/static/img/country_flags/rs.png"/>
</record>
</data>
</odoo>
In Odoo 18, data files are essential for effectively managing and organizing information. Using XML and CSV formats, developers can easily create and update records, which helps maintain a structured database.
Key operations like defining records, specifying fields, and invoking functions allow for customization and extension of Odoo’s functionalities. Understanding how to work with these data files is crucial for optimizing modules and improving the user experience. By mastering these tools, businesses can create a more efficient and tailored Odoo environment to meet their specific needs.