Odoo 16 Development Book

Function Call from XML

There may be some cases where you will need to perform some functions or methods once a module is installed. It is possible in Odoo to invoke a function from XML. The <function> tag is used to run python functions from XML. There are two ways to invoke a function from XML.

    1. Invoking function without parameters

    2. Invoking function with parameters

Invoking function without parameters

In this method, we can call a function defined inside any model without passing parameters. The syntax is given below.

<function id="function_id" model="model_name" name="method_name"/>

The model of the method is specified in the attribute model of the function tag. The other attributes are id and name. Id specifies the unique identifier of the XML record, and the name corresponds to the method name.

Let us see an example of a function call without parameters.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
	<function id="func_call_without_params" model="product.product"
               name="func_without_params"/>
    </data>
</odoo>

Here the model is product.product. So the system will invoke the method named func_call_without_params defined inside the model product.product. So the method definition will be as follows.

from odoo import api, fields, models
class product(models.Model):
   _inherit = "product.product"
   @api.model
   def func_without_params(self):
       self.create(dict(name='Test'))

After adding the XML data file inside the manifest, the method will be called on installing the module, and a new record will be created inside the model product.product.

Invoking function with parameters

The next method is Invoking function with parameters. In this method, we can call a function defined inside any model with passing parameters. The syntax is somewhat different from the previous one, and it is given below.

<function id="function_id"
          model="model_name"
          name="method_name">
    <value>param1</value>
    <value>param2</value>
    <!-- pass the parameters as per your need-->
</function>

Parameters for the method are passed inside value tag. It is possible to pass as many parameters as per your need. These parameters can be accessed from the method inside the model. An example is given below.

<odoo>
    <data noupdate="1">
    	  <function id="func_call_with_params"
               model="product.product"
               name="func_with_params">
            <value>Cybrosys Technologies</value>
        </function>
    </data>
</odoo>

Then the method definition need to be as follows.

from odoo import api, fields, models
class product(models.Model):
   _inherit = "product.product"
   @api.model
   def func_with_params(self, name):
       self.create(dict(name=name))

Then add the XML file inside the manifest. On installing the custom module, the python function will be invoked. The value of the parameter will be fetched, and this will be used to create the new product record.

location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message