DEMO DATAS FOR ODOO

Preparation of a test enviroment for Odoo

WHY OWN DEMO DATAS?

It often happens that we write an add-on that offers new features, but where standard demo data is not enough; or we want to give predefined settings to the add-on. In the cases mentioned, we have to expand the whole thing with demo data.

Data / demo data is mostly records, which integrate different entries into the database and logically link fields. By creating demo data, you can start a server in which, for example, already predefined users, products or orders are available and you have a live similar environment.

Normal odoo demo data is integrated via the XML files which are listed under __openerp__.py under the key "demo". This data is only imported if the user has clicked on the hook with demo data when creating the database.

The normal XML data is always added to the database during installation. Unless set with the "noupdate" flag (<data noupdate = "1">), the demo data will only be added to the system once during installation, without this tag the data will always be reset to its original value optimal test environment when testing something and modifying entries, where the data is easily restored through an update.

HOW DO I CREATE DEMODAT?

There are several ways to create demo data. On the one hand via XML records, CSV files or Yaml Records. Based on the res_partner model, I will show how to create the demo data for all three possibilities.

EXAMPLE OF A RES PARTNER FOR ODOO

XML FILE

As you can see here, adding this add-on by the data.xml adds a res.partner to the database. The noupdate = "1" flag will only do this once and not every time this add-on is updated. XML files always start with the entry <?Xml version="1.0" encoding="utf-8"?>, Which indicates the Odoo typical encoding of UTF-8. This is followed by the Odoo <openerp> entry, which then contains the data in <data>. Here are the records listed, which we want to add to the database.

The record first mentions the model responsible for the entry in the database. In our example of the res partner, this is the model res.partner. By <record model="res.partner" id = "res_partner_1"> a new entry with the ID res_partner_1 is added to the model.

Then follow the fields, which we want to fill with data. To add a name to the res.partner with the ID res_partner_1, we call the field name with <field name="name">Max Mustermann</field> and add it to Max Mustermann.
True or False is added by <field name="customer" eval="False"/> using eval. References to other fields or entries are added by <field name="company_id" ref="base.main_company"/> by specifying the field as a reference.
With large amounts of data you can reach fast, very long XML files, which is why we now come to the CSV files. In Odoo we can also read directly CSV files. This is especially advantageous for large data sets.

CSV FILE

In the CSV, the first line tells the system in which fields of the model we want to write the information into the database. In the following lines the information will be listed. It is important that in Odoo, the CSV files bear the name of the model in which you want to write the data. In our case this would be res.partner.csv.
CSV files do not have a "noupdate" flag and are overwritten each time during the update. But CSV data also have disadvantages: they do not have the same rights as XML files.
I was not able to create a user via a CSV file. This is also logical, because you can not use the backend (and CSV file), just a new administrator can import and get access to the system.

YAML FILE

The main advantage of demo data about Yaml is that the data is only volatile data in the database when needed for a test. They are then created for the test and deleted from the database after the test. Of course you can also use them as normal demo files by adding them in the __openerp__.py of the add-on in the section 'data':[].
Yaml Records are ideally combinable with Yaml tests, because in any case you want to test, you can create an entry without further ado. This record can then be changed by a test in one direction and then during the test, reset to the original state and tested in another direction.
For large amounts of records are not Yaml test advantageous because the overview is lost quickly.

SUMMARY

In XML files you can integrate permissions and functions into a demo file.
CSVs are ideal for large amounts of data because they can easily be created or extracted from the database.
Yaml files are ideal for testing, as this way you can easily create situation and entries in the database for a test.

EXPORT AND TRANSFORM LIVE DATA FROM CSV TO XML

From the Odoo system you can export very well entries from the database into a CSV. As I noted before, there are situations where you have to address certain fields differently or you do not have permissions to create entries in the database.
Here's an example about creating demo data, for automatically creating users in the database, which is only possible via XML.

SELECTION FOR EXPORT

We select users under the settings. Then we select the users that we want to export and then click on the "More" button to export the users.

FIELDS SELECTION

Odoo now gives us the opportunity to decide in advance which information we want to export. This is useful, for example, if we know that we do not need specific fields because they are set automatically when you create them.

EXPORT IN CSV

After selecting the fields, all we have to do is confirm with "Export To File" and Odoo will provide us with a CSV file. The CSV file immediately carries the correct name of the model in which we want to insert the demo data into our new database. This would be important if we want to use the CSV file as a demo file.

CONVERT THE CSV TO AN XML

We could now manually create an XML from the CSV. But with very large data, with hundreds or more entries, this would be very cumbersome and take a long time. I use online tools for such cases, which accelerate the process rapidly. With this converter one writes once the structure of the XML file to be created and afterwards all other records are written in an XML. It is also important to note what kind of field it is - in the following example I have marked the differences in color.

Odoo uses a comma (,) when exporting the CSV, to separate the data, and the data is written to Double Quotes ("), which must be reported to the converter.

IMPORTING DEMO DATAS

When importing demo data, care must be taken to ensure that entries and fields that you want to write already exist or are created. In our example, we can only create the user if we have previously created the partner. (field name="partner_id" ref="res_partner_1") The user needs a reference to a partner.
In the __openerp__.py then the following order must be followed for demo data, so that dependent entries

Demo datas that should only be installed if demo data is desired.

'demo': [
'demo/res.partner.xml',
'demo/res.users.xml',
],

Demo datas, which are always desirable, because they predefine settings for an add-on or create standard users.

'data': [
'demo/res.partner.xml',
'demo/res.users.xml',
],

WORKFLOWS

CREATING WORKFOWS AND FURTHER ASSOCIATED RECORDS

In Odoo you can also create more complex records via demo data by creating workflows or using existing ones.
If we have created an order, via a record, with all their connections, we can now tell the system through the workflow that it will be put into another status. As a result, demo data is created as if someone had created a sale order and, as in our example, this already confirmed.

Furthermore, we can extend this by giving the order a relationship with other Odoo modules. The code creates the order in the system as if it is considered paid, and the entire delivery process with invoices, goods receipts and goods issues is put into effect. The advantage is that the user receives demo data that are all connected to each other and how they were created in the live system. This provides a very good test environment because it involves real workflows.

Also a useful feature is, if you want to provide demodata with timecodes:
As a result, a time code is stored in the system and thus always receives temporally relevant demo data, no matter when you create the demo data.

It is also possible to overwrite records that are always automatically created in Odoo. For example, Odoo always creates the company with YourCompany.
So we can just override them with the following record. First, we overwrite the res.partner and then the base.main_company, because it references the res.partner.

As we now realize, our Main Company needs more dependencies. This is very important and must be taken into account when creating demo data, as otherwise they will emit errors and not be transferred to the system.
So we do not have the bank id, that means we add our bank data to our demo data and realize that this implies a further dependency. This can happen quite often and you can then shimmy from bottom to top until all necessary dependencies are given.

It is also possible to give dependencies to certain records via parent_id. Here we create three sales teams, two depending on the first one. This has the advantage that we create in a section dependency, over- and sub-categories.