Odoo is one of the data-driven applications which are used for business management and the vitality to describe the various aspects of company operational data is inevitable.
Here in this blog, we are going to discuss data files and operations in Odoo.
Module creation
We can begin with the module structure. The main part of module creation is the definition of different records which they will manage. As we all know, Odoo greatly depends on data therefore, it needs to manage menus and views of the user interface, access rights and access rules, different reports, and plain data. We can add data through XML and CSV files. Let's have a look at how we can define data in Odoo via XML files.
<?xml version=”1.0” encoding=”UTF-8”?>
<odoo>
<operation/>
……
</odoo>
This is the root element of the data files. We can use many operation elements within the root.
<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>
Here we can see a new attribute name in the data tag. (i.e), noupdate. Whenever we are adding data we must need to give noupdate = 1, since the contents of the file should be applied once when we install the module.
Next, we can move on to have a look at what are the core operations in a data file to create a record.
Record:
< record >…………..</ record >
A record tag in an XML file is used to define or update database records.
The below are some of the attributes of the record tag.
Model: This attribute is used to mention the name of the model which we need to create/update a record. That means we are going to create a record in this model.
ID: This indicates the external ID for the record. It will be used in record modifications in the future.
Context: It is used when we are creating records. Example:
<record id="main_partner" model="res.partner" context="{'default_is_company': True}">
Forcecreate: It is used in update mode whether the record should be created if it doesn’t exist. It requires an external ID and it is True by default.
Example:
<record forcecreate="True" id="decimal_price" model="decimal.precision">
<field name="name">Product Price</field>
<field name="digits">2</field>
</record>
After defining the model and ID of the record we need to define the fields which we need to set up. Fields are defined within a field tag ( <field name=”...” />). Otherwise, it will be taken as a default value on the creation. These are all the attributes for a record tag. Next, we are going to look at what are the attributes of a field tag.
Field:
The following are the attributes in the field tag:
Name: This attribute is used to mention the name of the field. It is a mandatory field.
Ref: In this attribute, we define an external ID of another record.
Type: This attribute is used to convert fields to convert formats. Base64, int, float, XML/HTML, file, list, tuple, char, and many more are the available types.
- XML, Html: It is used to extract the field’s children as a single document. And evaluates any external ID specified with the form %(external_id)s. %% can be used to output actual % signs.
- File: It ensures that the field content is a valid file path in the current model and saves the pair module path as the field value.
- Char: It sets the field content directly as the field’s value without alterations.
- Int: It converts the field’s content to an integer and sets it as the field’s value.
- Float: It converts the field’s content to a float and sets it as the field’s value.
- Base64: The base64-encodes the field’s content, use combined with the file attribute to load. Example; image data into attachments.
- List, tuple: They should contain any number of value elements with the same properties as a field, each element resolves to an item of a generated tuple or list, and the generated collection is set as the field’s value.
eval: It may contain various modules like time, delta-time, time delta, relative delta, and function to resolve external ID.
Delete:
Next, we are going to look at the delete tag and its attributes. If we want to delete a record that is previously defined, we can use the delete tags. Model, ID, Search are the attributes of the delete tag.
a) Model: From which model we need to delete the record, it must be mentioned on this attribute. It is a mandatory attribute.
b) ID: It indicates the external ID of the record which we are going to delete. It is an exclusive attribute.
c) Search: It is a domain to find the records of the model to delete. Search is also an exclusive attribute.
Function :
Next, we are going to have a look at the function tag. The function tag calls a method on a model with its provided attributes. Model, Name is the parameters of the function tag.
a) Model: It indicates the model where we are going to call the method.
b) Name: It indicates the name of the model where we are going to call the method.
Parameters can be provided using eval (should evaluate to a sequence of parameters to call the method with) or value elements (see list values).
<odoo>
<data noupdate ="1">
<record name="partner_1" model="res.partner">
<field name="name">Odude</field>
</record>
<function model="res.partner" name="send_inscription_notice"
eval="[[ref('partner_1'), ref('partner_2')]]"/>
<function model="res.users" name="send_vip_inscription_notice"
eval="[[('vip', '=', True)]]" model="res.partners" name="search"/>
<data>
<record id="model_form_view" model="ir.ui.view">
</record>
</odoo>
Up to this, we looked at how we can add data through an XML file. Next, we are focusing on how to add data through a CSV file. We are mainly using CSV files for adding data in the case of access rights.
Add data through a CSV file
For a better understanding of the operation, let’s take an example.
"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"
state_us_5, us, "California", "CA"
state_us_6, us, "Colorado", "CO"
The file name should be in the format of model_name.csv, and the first row lists the fields needed to be written. Each row after that indicates new records to be added.
In the above example, the first column is the external ID of the records to be created/updated. The second column is the ID of the country object to link to (country objects must have been defined beforehand). The remaining is the name field and code field for the corresponding model.