Odoo 16 Development Book

Data Loading

Odoo is mainly data-driven, and most operations greatly depend on data. In this section, we are going to discuss how to load data on module installation - the different ways and more operations related to Odoo data. There are two ways to load data in Odoo

  • Using XML files
  • Using CSV files

Xml Files

Data records can be defined in Odoo using XML files. For that data XML files are to be placed. First, we have to create a directory inside the module called data. data.xml files are commonly created inside the directory data. Let’s discuss how to create partner data using XML files. For that, let us create a new module called custom_contacts, inside which creates a new directory called data. Inside the data directory, create an XML file named res_partner_data.xml.The record data can be loaded using an XML file by following the syntax below.

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

For creating a record, it is necessary to give values for all required fields. We can create a partner record using the below XML code. The record is created using the record tag. Two attribute values can be set for the record tag: id and model. ‘Id’ is the unique identifier for this XML record. ‘Model’ specifies in which model the record is being created. In this example, we are creating a new record in the Contacts model; hence the model is given as res.partner. Inside the record, each field value is given using the field tag as given below. The Name specifies the technical name of the field, and after that, the field value is given.

<odoo>
        <data>
            <record id="partner_1" model="res.partner">
                <field name="name">Aby Wills</field>
                <field name="phone">9865432344</field>
                <field name="email">aby@example.com</field>
            </record>
        </data>
     </odoo>

After creating this data.xml file add this inside __manifest__.py file as follows.

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

After configuring this, install the module custom_contacts. A partner data with the specified values will be created in the database after installing the module.

CSV Files

CSV files are used in case of large data loading, as using XML files may not be suitable. In Odoo source modules itself, the usage of CSV files can be seen. Mainly they are used to define access rights. There are some important points to remember when using CSV files.

  • This file's name should be model_name.csv."ir.model.access.csv," for instance.ir.model.access is the model name for the access permissions.CSV files are also added inside the manifest.
  • The first row of the CSV file will have the fields that need to be written into, and we also have a field id for the external ID (used for creation or updation)

Let’s see this with the use of an example. For that, consider the account.tax.group.csv.

id,name,country_id/id
tax_group_0,TVA 0%,base.lu
tax_group_3,TVA 3%,base.lu
tax_group_6,TVA 6%,base.lu
tax_group_8,TVA 8%,base.lu
tax_group_10,TVA 10%,base.lu
tax_group_12,TVA 12%,base.lu
tax_group_14,TVA 14%,base.lu
tax_group_15,TVA 15%,base.lu
tax_group_17,TVA 17%,base.lu

The first row specifies the fields and is followed by values. Hence on installing the module consisting of this CSV file, the corresponding records inside the model account.tax.group will be created.

No Update

Odoo features a property named No Update for the element. The values in the XML file will be produced again whenever a module is altered or upgraded according to this parameter value. In some cases, the data files' contents can not be changed. At that moment, the noupdate property value can be set to 1.

<odoo>
        <data noupdate="1">
            <record id="partner_1" model="res.partner">
                <field name="name">Aby Wills</field>
                <field name="phone">9865432344</field>
                <field name="email">aby@example.com</field>
            </record>
        </data>
     </odoo>

Consider the above example. Since the noupdate attribute value is set to 1, any changes in this xml record will not be effected during the further upgrade process of the custom_contacts module. All the properties will be only effected at the time of the installation process. Hence using the noupdate attribute, we can make sure during the development process that the record created using the XML file is not updated frequently.

Force Create

Like noupdate, the forcecreate is also an attribute of the tag. By using this attribute, we can make sure that the record is being created if it doesn’t exist. The forcecreate attribute is true by default. This also requires an external ID of the record. Consider the example given below.

 <record forcecreate="True" id="decimal_payment" model="decimal.precision">
        <field name="name">Payment Terms</field>
        <field name="digits">6</field>
     </record>

Here, if there is no record found in the model decimal.precision with external id decimal_payment, a new record will be created.

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