Odoo has XML as the principal method of defining views, actions, security rules, and records/data. XML is essential in determining how modules operate and how users interact with the system. While XML is a formal declaration of what will happen when a user uses a module, developers often need to run Python code automatically when installing a module as a result of the business requirements of the module's user base.
The XML tag shows developers how to run a Python method directly from an XML definition during module installation or upgrade. With this feature, the developer is able to automatically execute all necessary setup steps for a module whenever it is installed and in multiple environments. It allows for the development of self-contained modules and provides a way to define initialization logic at the time the module is deployed.
Assuming you have created a custom module, you will need to create an XML file (often called data.xml) in your new module; this XML file contains the necessary instructions for Odoo to execute when installing the module.
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
</data>
</odoo>
The noupdate attribute plays an important role here. When noupdate="1" is set, the records and function calls inside the XML file are executed only once and will not be re-executed during subsequent module updates. This is particularly important when calling functions, as it prevents duplicate record creation or repeated execution of initialization logic. If noupdate="0" is used, the XML content will be reloaded on every module update.
Remember to reference this XML file in your module’s __manifest__.py so that Odoo will load it during installation.
The next step is to create the Python method that is referenced in the XML. This method must be defined inside the model specified in the XML.
from odoo import models, api
class ProductProduct(models.Model):
_inherit = 'product.product'
@api.model
def fun_invoke_without_param(self):
self.create({
'name': 'Test Product'
})
In this case, the method creates a new product record. After defining this method, it can be called from the XML file with the function tag.
<function model="product.product"
name="fun_invoke_without_param" />
When Odoo installs this module, it will automatically call this function and create the product record without any further action from the user.
Passing parameters to a function provides a way to add flexibility to that function. Odoo allows you to pass parameters using the tag in the block.
@api.model
def fun_invoke_with_param(self, product_name):
self.create({
'name': product_name
})
You can create a link between your XML file and the method in Odoo by passing the required values as shown below:
<function model="product.product"
name="fun_invoke_with_param">
<value>Cybrosys Technologies</value>
</function>
When calling a method from XML, the values you pass must be passed to the method in the same way they are defined in the function signature. This allows you to reuse the same method for multiple initialization needs regardless of the method being called.
The following is a complete example of the data.xml file that invokes both methods:
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
<function model="product.product"
name="fun_invoke_without_param" />
<function model="product.product"
name="fun_invoke_with_param">
<value>Cybrosys Technologies</value>
</function>
</data>
</odoo>
Adhering to the best practices of using the tag is very beneficial. You should make the methods lightweight and efficient; any heavy work done inside them may negatively impact the speed of installing the module. Properly handling errors ensures that the installation does not fail.
In summary, the use of the tag to invoke Python functions from XML files is an effective way for developers to automate initialization tasks, ensure consistency, and simplify module deployment in Odoo 19. By combining the XML configuration with the underlying logic of Python effectively, you can create high-quality Odoo modules that are both maintainable and ready for production use.
To read more about How to Invoke Functions from XML Files in Odoo 18, refer to our blog How to Invoke Functions from XML Files in Odoo 18.