E-Book, Englisch, 214 Seiten
Reis Odoo Development Essentials
1. Auflage 2025
ISBN: 978-1-78439-210-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
Fast track your development skills to build powerful Odoo business applications
E-Book, Englisch, 214 Seiten
ISBN: 978-1-78439-210-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
This book is intended for developers who need to quickly become productive with Odoo. You are expected to have experience developing business applications, as well as an understanding of MVC application design and knowledge of the Python programming language.
Autoren/Hrsg.
Weitere Infos & Material
XML data files
While CSV files provide a simple and compact format to serialize data, XML files are more powerful and give more control over the loading process.
We have already used XML data files in the previous chapters. The user interface components, such as views and menu items, are in fact records stored in system models. The XML files in the modules are a means used to load those records into the server.
To showcase this, we will add a second data file to the module, named , with the following content:
This XML is equivalent to the CSV data file we have just seen in the previous section.
XML data files have a element containing elements, inside of which we can have have several elements, corresponding to the CSV data rows.
A element has two mandatory attributes, and (the external identifier for the record), and contains a tag for each field to write on.
Note that the slash notation in field names is not available here: we can't use . Instead the special attribute is used to reference External IDs. We'll discuss the values for the relational "to many" fields in a moment.
The data noupdate attribute
When the data loading is repeated, existing records from the previous run are rewritten.
This is important to keep in mind: it means that upgrading a module will overwrite any manual changes that might have been made to the data. Notably, if views were modified with customizations, those changes will be lost with the next module upgrade. The correct procedure is to instead create inherited views for the changes we need, as discussed in the Chapter 3, .
This overwrite behavior is the default, but it can be changed, so that when an already created record is loaded again no change is made to it. This is done by adding to the element a attribute. With this, its records will be created the first time they are loaded, and in subsequent module upgrades nothing will be done to them.
This allows for manually made customizations to be safe from module upgrades. It is often used with record access rules, allowing them to be adapted to implementation specific needs.
It is also possible to have more than one section in the same XML file. We can take advantage of this to have a data set with and another with .
The flag is stored in the External Identifier information for each record. It's possible to edit it directly using the External Identifier form available in the Technical menu, with the Non Updatable checkbox.
Tip
The e attribute is tricky when developing modules, because changes made to the data later will be ignored, and Odoo won't pick up later modifications. A solution is to keep " during development and only set it to once finished.
Defining Records in XML
Each element has two basic attributes, and , and contains elements assigning values to each column. As mentioned before, the attribute corresponds to the record's External ID and the to the target model where the record will be written. The elements have available a few different ways to assign values. Let's look at them in detail.
Setting field values
The element defines a data record, and contains elements to set values on each field.
The attribute of the field element identifies the field to be written.
The value to write is the element content: the text between the field's opening and closing tag. In general this is also suitable to set non-text values: for Booleans, or values will be correctly converted; for dates and datetimes, strings with and will be converted properly.
Setting values using expressions
A more advanced alternative to define a field value is using the attribute instead. This evaluates a Python expression and assigns the resulting value to the field.
The expression is evaluated in a context that, besides Python built-ins, also has some additional identifiers available. Let's have a look at them.
To handle dates, the following modules are available: , , and . They allow calculating date values, something that is frequently used in demonstration (and test) data. For example, to set a value to yesterday we would use:
Also available in the evaluation context is the function, used to translate an External ID into the corresponding database ID. This can be used to set values for relational fields. As an example, we have used it before to set the value for the :
The evaluation context also has a reference available to the current Model being written through . It can be used together with to access values from other records. Here is an example from the Sales module:
Setting values for relation fields
We have just seen how to set a value on a many-to-one relation field, such as , using the attribute with a function. But there is a simpler way.
The element also has a attribute to set the value for a many-to-one field using an External ID. Using it, we can set the value for using just:
For one-to-many and many-to-many fields, a list of related IDs is expected, so a different syntax is needed, and Odoo provides a special syntax to write on this type of fields.
The following example, taken from the Fleet app, replaces the list of related records of a field:
To write on a to many-field we use a list of triples. Each triple is a write command that does different things according to the code used:
- : This creates a new record and links it to this one
- : This updates values on an already linked record
- : This unlinks and deletes a related record
- : This unlinks but does not delete a related record
- : This links an already existing record
- : This unlinks but does not delete all linked records
- : This replaces the list of linked records with the provided list
The underscore symbol used above represents irrelevant values, usually filled with or .
Shortcuts for frequently used Models
If we go back to Chapter 2, , we can find in the XML files elements other than , such as and .
These are convenient shortcuts for frequently used Models that can also be loaded using regular elements. They load data into base Models supporting the user interface and will be explored in more detail later, in Chapter 6, .
For reference, so that we can better understand XML files we may encounter in existing modules, the following shortcut elements are available with the corresponding Models they load data into:
- : This is the Window Actions model
- : This is the Menu Items model
- : This is the Report Actions model
- :...




