Reader small image

You're reading from  Odoo 15 Development Essentials - Fifth Edition

Product typeBook
Published inFeb 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800200067
Edition5th Edition
Languages
Tools
Right arrow
Author (1)
Daniel Reis
Daniel Reis
author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis

Right arrow

Chapter 5: Importing, Exporting, and Module Data

Most Odoo module definitions, such as user interfaces and security rules, are data records that are stored in specific database tables. The XML and CSV files that are found in modules are not used by Odoo applications at runtime. They are a means of loading those definitions into database tables.

Because of this, an important part of Odoo modules is representing data in files so that it can be loaded into a database upon module installation. Modules can also contain initial data and demonstration data. Data files allow us to add that to our modules.

Additionally, understanding Odoo data representation formats is important for exporting and importing business data within the context of a project's implementation.

The following topics will be covered in this chapter:

  • Understanding the external identifier concept
  • Exporting and importing data files
  • Using CSV files
  • Adding module data
  • Using XML data files...

Technical requirements

This chapter requires you to have an Odoo server running, with the library app base module installed.

The code for this chapter can be found in this book's GitHub repository, https://github.com/PacktPublishing/Odoo-15-Development-Essentials, in the ch05/ directory. It contains a copy of the original library_app that we created in Chapter 3, Your First Odoo Application, with additional files added for this chapter.

Understanding the external identifier concept

An external identifier, also called an XML ID, is a human-readable string identifier that uniquely identifies a particular record in Odoo. They are important for loading data into Odoo, allowing us to modify an existing data record or reference it in other data records.

First, we will introduce how external identifiers work, and how we can inspect them. Then, we will learn how to use the web client to find the external identifiers for particular data records, since this is frequently needed when creating add-on modules, thus extending existing features.

How external identifiers work

Let's begin by understanding how identifiers work. The actual database identifier for a record is an automatically assigned sequential number, and there is no way to know ahead of time what ID will be assigned to each record during module installation. External identifiers let us reference a related record without the need to know the actual database...

Exporting and importing CSV data files

An easy way to generate data files and get insight into what structure the files should have is to use the built-in export feature.

With generated CSV files, we can learn about the format that's needed to import data manually into the system, edit them to perform mass updates, or even use them to produce demo data for our add-on modules.

In this section, we will learn about the basics of exporting and importing data from Odoo's user interface.

Exporting data

Data exporting is a standard feature that's available in any list view. To use it, we must pick the rows to export by selecting the corresponding checkboxes, on the far left, and then selecting the Export option from the Action button at the top of the list.

First, we should add a couple of Odoo books to the Library app, along with their publishers and authors. For this example, we created Odoo Development Essentials 11 and Odoo 11 Development Cookbook.

We...

Adding module data

Modules use data files to load their default data, demonstration data, user interface definitions, and other configurations into the database. For this, we can use both CSV and XML files.

Changes in Odoo 12

The YAML file format was also supported until Odoo 11 and was removed in Odoo 12. Still, for a usage example, you can look at the l10n_be official module in Odoo 11, and for information on the YAML format, you can visit http://yaml.org/.

CSV files that are used by modules are the same as those we have seen and used for the import feature. When using them in modules, the filename must match the name of the model that the data will be loaded into. For example, a CSV file for loading data into the library.book model must be named library.book.csv.

A common usage of data CSV files is for accessing security definitions that have been loaded into the ir.model.access model. They usually use CSV files in a security/ subdirectory, named ir.model.access.csv...

Using XML data files

While CSV files provide a simple and compact format to represent data, XML files are more powerful and give more control over the loading process. For example, their filenames are not required to match the model to be loaded. This is because the XML format is much richer and more information regarding what to load can be provided through the XML elements inside the file.

We used XML data files in the previous chapters. The user interface components, such as the views and menu items, are, in fact, records that are stored in system models. The XML files in the modules are used to load these records into the instance database.

To showcase this, a second data file will be added to the library_app module, data/book_demo.xml, with the following content:

<?xml version="1.0"?>
<odoo noupdate="1">
  <!-- Data to load -->
  <record model="res.partner" id="res_partner_huxley"> 
&...

Summary

In this chapter, we learned how to represent data in text files. These can be used to manually import data into Odoo or include it in add-on modules as default or demonstration data.

At this point, we should be able to export and import CSV data files from the web interface and leverage external IDs to detect and update records that already exist in the database. They can also be used to perform a mass edit on data, by editing and reimporting a CSV file that has been exported from Odoo.

We also learned about how XML data files are structured, and all the features they provide, in more detail. These were not only set values on fields but also actions such as deleting records and calling model methods.

In the next chapter, we will focus on how to use records to work with the data contained in models. This will give us the necessary tools to then implement our application's business logic and rules.

Further reading

The official Odoo documentation provides additional resources on data files: https://www.odoo.com/documentation/15.0/developer/reference/backend/data.html.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Odoo 15 Development Essentials - Fifth Edition
Published in: Feb 2022Publisher: PacktISBN-13: 9781800200067
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis