Odoo’s XML-based view system provides a powerful, declarative way to define data models and user interfaces. In practice, though, enterprises often run into scenarios where static XML definitions aren’t enough — particularly when working with large or changing datasets or when a module install must perform changes to existing records. In these cases, the XML <function> tag is a useful tool: it lets you invoke Python methods at install time so necessary updates or initialization steps occur automatically during module installation. This article walks through how to apply the <function> tag step by step to ensure post-install operations run reliably in production.
To trigger a Python method from an XML file in Odoo, you first need to create an XML file, commonly named data.xml, which will hold the required data declarations and function calls. This file acts as the blueprint for any operations that should run automatically when the module is installed, including the execution of your custom method.
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
</data>
</odoo>
The ‘noupdate’ attribute determines whether the data defined in an XML file should be reapplied when a module is updated. When noupdate="1" is used, commonly for demo or initial setup data. Odoo will not re-import those records if they already exist or were manually removed before an update. If the record is missing, however, Odoo will recreate it. In contrast, setting noupdate="0" forces Odoo to re-import the data on every module update, ensuring the records are always refreshed.
This mechanism prevents certain XML actions, such as function calls or data loading, from being executed repeatedly during upgrades. Finally, don’t forget to reference this XML file in your module’s __manifest__.py so it is correctly loaded during installation.
There are two primary ways to trigger a Python method from XML in Odoo:
- Calling a function without passing any arguments.
- Calling a function while supplying arguments.
1. Calling a function without passing any arguments.
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data>
<function model="res.partner" name="_create_new_partner"/>
</data>
</odoo>
Inside the <data> tag, you can include a <function> element, which Odoo uses to execute a method during module installation or update. In this example, the function points to _create_new_partner, and the model attribute tells Odoo which model the method belongs to—in this case, res.partner. When this function is triggered, it performs the logic defined in the method, such as creating a new partner or contact record in the database.
Now, create a Python file that inherits the model where you want to implement the method. This inherited model will contain the custom function that will be triggered from the XML file.
# -*- coding: utf-8 -*-
from odoo import api, models
class ResPartner(models.Model):
_inherit = "res.partner"
@api.model
def _create_new_partner(self):
""" Function invoked from xml """
self.create(dict(name='New partner'))
A new record will be created when the function is called through xml.
2. Calling a function with arguments.
We can perform a function by providing the arguments. The xml will look like:
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data>
<function model="res.partner"
name="fun_invoke_with_param">
<value>Cybrosys Technologies</value>
</function>
</data>
</odoo>
Within the <function> element, a <value> tag holds the text “Cybrosys Technologies”, which is passed as an argument to the func_with_params method. This allows the function to receive and use the provided value during execution. Add the function in the model you have already inherited.
@api.model
def fun_invoke_with_param(self, name):
""" Function invoked from xml """
self.create(dict(name=name))
The func_with_params method requires an argument, specifically a name. Using Odoo’s create method, the function adds a new entry to the res.partner model. The value passed as the parameter is then used to populate the name of the newly created record.
Conclusion
The <function> tag in Odoo XML files offers a simple way to run Python methods during module installation or updates. Whether you’re calling a method with or without parameters, this technique lets you automate tasks like creating or updating records. By pairing XML with custom Python logic, you can ensure key operations run smoothly as soon as the module is installed.
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.