In many situations, we may need to add demo data to our custom modules. Here, in this blog, we are going to discuss how we can add demo data through XML and CSV files.
Let’s get started.
We can begin with the module structure itself. If you need to add demo data to your module, always create a separate folder named data/demo. In the manifest file, you should provide the path inside the manifest field ‘demo’. When we specify any files inside this demo field in the manifest- the data is only installed if the ‘Load Demo Data’ option is enabled at the time of database creation, otherwise, the demo data will not be loaded.
Here I am going to create some employees through my custom module. For that, I created a directory named data in that I added an XML file that contains employee data.
<?xml version="1.0" encoding="utf-8"?> <odoo> <data noupdate="1"> <!--DEPARTMENT DEMO DATA --> <record id="dep_management_test" model="hr.department"> <field name="name">DEMO DEPARTMENT</field> </record> <!--JOB DEMO DATA --> <record id="job_demo_data" model="hr.job"> <field name="name">DEMO JOB</field> <field name="department_id" ref="dep_management_test"/> <field name="description">Demo Description</field> <field name="requirements">Demo Requirements</field> </record> <!--EMPLOYEE CATEGORY DEMO DATA --> <record id="employee_category_demo" model="hr.employee.category"> <field name="name">DEMO CATEGORY</field> <field name="color" eval="1"/> </record> <!--EMPLOYEE DEMO DATA ? <record id="employee_demo" model="hr.employee"> <field name="name">DEMO EMPLOYEE</field> <field name="department_id" ref="dep_management_test"/> <field name="job_id" ref="job_demo_data"/> <field name="category_ids" eval="[(6, 0, [ref('employee_category_demo')])]"/> <field name="work_location">Building 1, Second Floor</field> <field name="work_phone">(376)-310-7863</field> <field name="work_email">ronnie.hart87@example.com</field> <field name="image_1920" type="base64" file="hr/static/img/employee_al-image.jpg"/> </record> </data> </odoo>
Here you can see -before creating an employee we should create other records such as department, job, employee category, and so forth. It may vary according to our requirements. Here, I am creating employees with these values. Before going to that you can see a new attribute in the data tag namely noupdate.
<data noupdate="1">
If we add demo data we must ensure to give noupdate=1 since the contents of this data file should be applied once while we install the module.
There are different attributes for creating records
Id
The external id for this record
Model
They have to name the model. We will also be creating a record in this model
These are the main attributes of the record. After adding this we should define the fields that we want to set. This is provided by using the field tag. If we didn’t define a field tag it will take the default values on creation.
Now we can check what attributes are used when we define field tags.
name
The name of the field.
Ref
It must be an external id of another record.
Eval
Which may contain external identifiers, various modules (time, DateTime, time delta, relative delta), etc.
Type
If we define a type attribute it will be used to convert the fields to convert to the format. The available types are
> Base64
> Int
> Float
etc.
This is how we create simple demo data. After that, we are adding this XML file to the manifest file.
'demo': [ 'data/hr_employee_demo.xml']
Now we can check how we can import data through CSV files. We can import the same things that we did above through the XML file.
When we check the above data we are creating data in
‘hr.job’, ’hr.department’, ’hr.employee.category’, ’hr.employee’ models.
So we should create separate CSV files for these models and the name of the file should be model_name.csv. Here we will be having 4 CSV files namely
* hr.department.csv
* hr.job.csv
* hr.employee.category.csv
* hr.employee.csv
Now we can check how to add data in CSV files. Here the first row contains the external id, followed by other fields.
hr.department.csv file
id,name
dep_management_test,DEMO DEPARTMENT 1
dep_management_test_2,DEMO DEPARTMENT 2
dep_management_test_3,DEMO DEPARTMENT 3
the second column is the name field of the ‘hr.department’
hr.job.csv
id,name,department_id:id,description,requirements
job_demo_data_1,demo job 1,dep_management_test,DEMO DESCRIPTION 1,demo requirement 1
job_demo_data_2,demo job 2,dep_management_test_2,DEMO DESCRIPTION 2,demo requirement 2
The second column is the name field of the ‘hr.department’.
The third column contains the external id of the department.
The fourth column contains the description field of the ‘hr.department’
hr.employee.category.csv
id,name
categ_1,DEMO CATEGORY 3
categ_2,DEMO CATEGORY 4
hr.employee.csv
id,name,department_id:id,job_id:id,category_ids:id,work_phone
test_employee,test_employee,dep_management_test,job_demo_data_1,categ_1,54221444
When we provide an external id for a field we must use just like this ‘field_name: id’ in the first row otherwise field name is enough.
After the data is configured don’t forget to add this filename inside the demo field in the manifest file.