Chapter 5 - Odoo 15 Development Book

Data Loading

Odoo is one of the data-driven applications used for business management. Odoo greatly depends on data. The data can be loaded in Odoo using XML and CSV files.

XML Files

Using XML files we can define data in Odoo. First we need to create a directory ‘data’, and a XML file (let’s say res_partner_data.xml) inside the directory ‘data’. Then we need to add the XML file to __manifest__.py as follows:-

'data' : [ 'data/res_partner_data.xml' ] 

Then we need to add contents to the XML file. The data can be loaded via XML as records. The syntax of creating the data as record is given below:-

<record model="model_name" id="record_id"> <field
                            name="field_name">field_data</field> </record> 

Here the model refers to the name of the model to which the data needs to be loaded, and the id refers to the unique identifier for the record. The <field> contains the fields in the model, here we can specify the field with the name and value to load data. For example we are creating a partner data as follows:-

<odoo>
      <data>
		<record id="res_partner_demo" model="res.partner">
			<field name="name">Demo </field>
			<field name="phone">4546546532</field>
			<field name="email">demo@gmail.com</field>
		</record>
	</data>
</odoo>

While we install the custom module with this data, a partner data will be loaded to the database with the specified fields.

CSV Files

XML data files are flexible and self-descriptive, but in the case of a large amount of data, it is not suitable for loading. In this case, CSV files are used most of the time. Mainly CSV files are used for defining access rights. The important points to note while using CSV files are:-

  • The file name should be ‘model_name.csv.’ E.g.:- ‘ir_model_access.csv’ The ir_model_access is the model name for the access rights. The CSV files are added in manifest inside the data files.
  • The first row in the CSV file will be the fields to write, additionally we have a field id for the external ID(used for creation or updation)

Let us look the data can be added through a CSV file,

"id", "country_id:id", "name", "code"
state_au_1, au, "Australian Capital Territory", "ACT"
state_au_2, au, "New South Wales", "NSW"
state_au_3, au, "Northern Territory", "NT"
state_au_4, au, "Queensland", "QLD"
state_au_5, au, "South Australia, "SA"
state_au_6, au, "Tasmania", "TAS"
state_au_7, au, "Victoria", "VIC"
state_au_8, au, "Western Australia", "WA"
state_us_1, us, "Alabama", "AL"
state_us_2, us, "Alaska", "AK"
state_us_3, us, "Arizona", "AZ"
state_us_4, us, "Arkansas", "AR"

Here the first column specifies the external ID of the record that needs to be created/ updated. The next column is the country ID link (country records must be imported before this). The name and code fields are the other fields in the corresponding model res.country.

No Update

No Update is an attribute of the <data> tag in Odoo. This parameter is sed for indicating that the values in the XML file must be recreated hen a module is updating. In some scenarios, the contents of data files is expected to be applied only once. At that time, we can specify the noupdate attribute to 1. An example of noupdate is as follows:-

<odoo>
	<data noupdate="1">
		<record id="res_partner_new" model="res.partner">
			<field name="name">Test Partner</field>
			<field name="email">test@gmail.com</field>
		</record>
	</data>
</odoo>

Considering the above given example while installing the module a partner Test is created inside the model res.partner. Any changes we are given for the record will not affect during the update of the module. This means, in the above-given code, if we change the email and upgrade the module, the email will not be changed.

Force Create

The forcecreate is an attribute of the <record> tag. Forcecreate is used when we update the module whether the record should be created if it doesn’t exist. It requires an external ID of the record, and it is true by default.

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

Considering the above given code, if the record with XML ID ‘decimal_price’ does not exist in the ‘decimal.precision’ model, then it will be created while updating the module.

External IDS and Namespaces

An External ID (also called XML IDs) is used to identify records in Odoo. The XML IDs are records of ‘ir.model.data’. They are important in the case of importing data in odoo.

While importing data using XML ID, it first checks for XML ID's existence. If it exists, then the UPDATE is working. If it does not exist, then it will perform the CREATE operation. Usually, an XML ID is a combination of the module field and the name field of the ir.model.data model. If the External ID is known then we are able to update the data, for eg:-

<?xml version="1.0" encoding="utf-8"?>
<odoo>
      <record id="base.main_company"" model="res.company">
        	<field name="name">Main Company</field>
    	</record>
</odoo>

As per the above given Odoo checks there is any record with the external id ‘base.main_company’ inside ‘res_company’. If found, then the name will be rewritten to Main Company. The XML ID can be accessed from UI as by turning on the developer mode and selecting the view Metadata option from debug tools.

While using the External ID on a <record> tag, Odoo checks whether the XML ID is namespaced (if it contains exactly one dot). If it is not namespaced then Odoo adds the current module name as a namespace.

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