Enable Dark Mode!
data-files-core-operations-in-odoo-16.jpg
By: Ayana KP

Data Files Core operations in Odoo 16

Technical Odoo 16

Odoo, a powerful open-source ERP platform, relies on data files as a crucial component for managing and structuring information within the system. 
Here in this blog, we are going to discuss data files and operations in Odoo 16

Structure

The core aspect of module creation revolves around defining the records that the module will manage. Data can be conveniently added to Odoo using XML and CSV files. Now, let's explore how we can precisely define data in Odoo by leveraging XML files.

<?xml version="1.0" encoding="utf-8"?>
<!-- the root elements of the data file -->
   <odoo>
       <operation/>
           ......
   </odoo>

The above example is the root element of the data file. Numerous numbers of operation elements can be used in root element odoo

<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>

If part of the data in the file is expected to be applied once, we can specify the odoo flag noupdate set to 1. we can place this part of the file in a <data noupdate=”1”> domain.

Core Operations

1. Record

<record>
   ...........
</record>

The <record> tag in an XML file is utilized to define or update database records in Odoo. Some of the commonly used attributes of the <record> tag are:

id: This attribute specifies a unique identifier for the record. It is used to reference the record in other parts of the XML file or in code.

model: It specifies the Odoo model or database table to which the record is associated. By including the model attribute, you establish the connection between the record and its corresponding model, enabling the system to handle and manage the record according to the predefined structure and behavior defined within the model.

context: It is used when we are creating records.

Eg:

<record id="main_partner" model="res.partner" context="{'default_is_company': True}">

forcecreate: This attribute determines whether the record should be created when in update mode, based on its existence in the database. It requires an external ID and it is True by default. 

Eg:

<record forcecreate="True" id="decimal_price" model="decimal.precision">
   <field name="name">Product Price</field>
   <field name="digits">2</field>
</record>

Once the model and ID of the record are defined, the next step is to specify the fields that need to be set up for that record. Fields are defined within a field tag ( <field name=”...” />). Otherwise, it will be taken as a default value on the creation. 

2. Field

The <field> tag allows you to set or update the values of fields for a particular record.

name: This attribute is an essential attribute used to specify the field's name. It is a required field that must be provided when defining a field in Odoo.

ref: This attribute is used for reference to another record by its external id. It enables the establishment of relationships between records in Odoo,

type: The type attribute in Odoo is used to interpret and convert the content of a field to a specific data type. It allows you to define how the field's content should be processed and represented.Odoo provides various built-in types that can be used with the type attribute. Here are some of the available types:

Base64: This type is used to encode and decode the field's content using Base64 encoding. It is often combined with the file attribute to handle file attachments.

Int: The Int type converts the field's content to an integer data type. It is used for storing and processing numeric values without decimal points.

Float: The Float type converts the field's content to a floating-point number data type. It is suitable for handling decimal or fractional values.

XML and HTML: The field's child elements are combined into a cohesive document. If any external ID is provided in the format %(external_id)s, it is assessed, and the use of %% allows for the inclusion of literal percent (%) signs in the output

File: The File type interprets the field's content as a file path within the current model. It is used for managing and handling file attachments.

List and Tuple: These types are used to define collections of values with similar properties. They allow you to store and retrieve multiple related values as a list or tuple.

Char: The Char type represents character or string data. It is used for storing and processing textual content.

eval : It allows you to perform dynamic computations or evaluations on field values or expressions. It enables you to execute Python code to generate a value or manipulate existing field values.

Eg:

<field name="company_ids" eval="[Command.link(ref('main_company'))]"/>

3. Delete 

The <delete> tags are used to specify the record or records that should be deleted from the database during the module upgrade process. Model, ID, Search are the attributes of the delete tag.

Model: The model in which a specified record should be  deleted

Id: The external id of a record to remove

Search: A domain to find records of the model to remove, id and search is also an exclusive attribute

Eg:

<delete model="account.fiscal.position.template" search="[('chart_template_id','=',ref('l10n_fr_pcg_chart_template'))]"/>

4. Function 

It can be used to call a method on a model with provided parameters. It has two mandatory parameters. 

name : The name of the method to call.

model : The model of the method to call.

parameters for a method in Odoo can be provided using the <eval> and <value> elements within XML files.

Eg:

<odoo>
   <data noupdate="1">
       <function name="install_lang" model="res.lang"/>
   </data>
   <data>
       <record id="base.lang_my" model="res.lang">
           <field name="url_code">mya</field>
       </record>
       <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, data files are used to manage large sets of data for various modules and perform initial data setups or updates efficiently.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message