Chapter 4. Data Serialization and Module Data

Most Odoo configurations, from user interfaces to security rules, are actually data records stored in internal Odoo tables. The XML and CSV files found in modules are not used to run Odoo applications. They are just a means to load those configurations into the database tables.

Because of this, an important part of Odoo modules is about representing (serializing) that data into files so that it can be later loaded into a database.

Modules can also have initial and demonstration (fixture) data. Data serialization allows adding that to our modules. Additionally, understanding Odoo data serialization formats is important in order to export and import data in the context of a project implementation.

Before we go into practical cases, we will first explore the external identifier concept, which is the key to Odoo data serialization.

Understanding external identifiers

All records in the Odoo database have a unique identifier, the id field.

It is a sequential number automatically assigned by the database. However, this automatic identifier can be a challenge when loading interrelated data: how can we reference a related record if we can't know beforehand what database ID will be assigned to it?

Odoo's answer to this is the external identifier. External identifiers solve this problem by assigning named identifiers to the data records to be loaded. A named identifier can be used by any other piece of record data to reference it later on. Odoo will take care of translating these identifier names into the actual database IDs assigned to them.

The mechanism behind this is quite simple: Odoo keeps a table with the mapping between the named External IDs and their corresponding numeric database IDs. That is the ir.model.data model.

To inspect the existing mappings, go to the Technical section of the Settings menu, and select the Sequences & Identifiers | External Identifiers menu item.

For example, if we visit the External Identifiers list and filter it by the todo_app module, we will see the external identifiers generated by the module created previously.

Understanding external identifiers

You can see that the external identifiers have a Complete ID label. This is composed of the module name and the identifier name joined by a dot, for example, todo_app.action_todo_task.

Since only the Complete ID is required to be unique, the module name ends up acting as a namespace for identifiers. This means that the same named identifier can be repeated in different modules, and we don't need to worry about identifiers in our module colliding with identifiers in other modules.

At the top of the list, you can see the todo_app.action_todo_task ID. This is the menu action we created for the module, which is also referenced in the corresponding menu item. By clicking on it, you can open a form with its details: the action_todo_task in the todo_app module maps to a specific record ID in the ir.actions.act_window model.

Understanding external identifiers

Besides providing a way for records to easily reference other records, External IDs also allow avoiding data duplication on repeated imports. If the External ID is already present, the existing record will be updated, instead of creating a new record. This is why, on subsequent module upgrades, previously loaded records are updated instead of being duplicated.

Finding External IDs

When preparing configuration and demonstration data files for modules, we frequently need to look up existing External IDs that are needed for references.

We can use the External Identifiers menu shown earlier, but the Developer Menu can provide a more convenient method for that. As you may recall from Chapter 1, Getting Started with Odoo Development, the Developer Menu is activated in the About Odoo option, and then, it is available at the top-left corner of the web client view.

To find the External ID for a data record, on the corresponding Form view, select the View Metadata option from the Developer Menu. This will display a dialog with the record's database ID and External ID (also known as XML ID).

As an example, to look up the Demo user ID, we can navigate to its Form view (Settings | Users) and select the View Metadata option, after which we will be shown this:

Finding External IDs

To find the External ID for view elements, such as form, tree, search, and action, the Developer Menu is also a good help. For that, use its Manage Views option or open the information for the desired view using the Edit <view type> options, and then select their View Metadata option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset