Chapter 5 - Odoo 15 Development Book

Function call from XML

By using <function> tag, we can call methods from XML files while installing the custom modules. Sometimes we need to perform some functions or methods just after the installation of custom modules. In this case <function> tag is used for invoking function calls from XML files. First we need to create a demo data file as follows:- data.xml

 <?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
    </data>
</odoo>

Then we need to add this data.xml inside the __manifest__.py file.

The function can be invoked as given below:-

1. Invoking Function without parameters

First we need to define a function in the model as given below:-

 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'))

So now in order to call the function from an XML file we need to create a <function> tag in the data.xml. The syntax is as follows:

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

Here the id refers to the unique identifier of the function, model refers to the model which contains the function or method to invoke and the method name specifies the name of method to be called from the XML file. The function func_without_params is invoke from XML as given below;-

<?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>

Once the module is installed the function will be invoked.

2. Invoking Function with parameters

We can define the function with parameters 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))

For call this function from XML file we need to create a <function> tag and can specify the parameters using <value> tag as below:-

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

By using the <value> tag we can pass the parameters to the function. Now the data.xml is as follows:-

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

After installation of the module the func_with_params will be invoked with the value to the parameter as Cybrosys Technologies.

whatsapp
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