Enable Dark Mode!
how-to-invoke-functions-from-xml-files-in-odoo-18.jpg
By: Anurudh P

How to Invoke Functions from XML Files in Odoo 18

Technical Odoo 18

In Odoo, views are typically designed using XML, enabling the creation of various types of data structures and interfaces. While XML is a powerful tool, challenges often arise in real-world business scenarios, especially when dealing with dynamic or large datasets. For instance, in certain situations, you might need to update existing records automatically when a module is installed in a production environment.

This article guides using the <function> tag in XML files to call methods during the installation of your Odoo modules. Often, specific operations must be executed immediately after a module is installed. In such scenarios, the <function> tag can trigger the desired methods or functions, ensuring these operations are performed seamlessly during installation.

Let's explore how to achieve this step by step.

To call a function from XML files in Odoo, you must first create an XML file, such as data.xml, to define the necessary data and functions. This file will serve as the container for the instructions on executing your method during module installation.

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

The noupdate parameter is used to control whether the values in the XML files should be reimported during a module update. For example, when noupdate="1" is set in a file containing demo data, the demo record will not be reimported if it is deleted before the module update. However, if the record is absent from the database, it will be reimported. On the other hand, if noupdate="0" is set, the record will always be reimported during an update. This parameter ensures that the XML is not executed repeatedly when updating the module. Additionally, make sure to include this data file in the __manifest__.py of your module to ensure it is properly loaded.

You can invoke a function using this method, as shown below:

1. Invoking function without parameter

First, you need to define a function in your model. Here's an example of how it can be done:

# -*- coding: utf-8 -*-
from odoo import models, fields, api

class ProductDetails(models.Model):
   _inherit = 'product.product'
   @api.model
   def fun_invoke_without_param(self):
       self.create(dict(name='test'))

To call the function you created, you can use the <function> tag in the XML data file as shown below:

2. Invoking a Function with Parameters

Here’s how you can define a function with parameters:

@api.model
def fun_invoke_with_param(self, name):
   self.create(dict(name=name))

To call these functions, you can use the <function> tag in the XML file and pass the required parameters using the <value> tag, as shown below:

<function model="product.product"
         name="fun_invoke_with_param">
   <value>value 1</value>
   <value>value 2</value>
</function>

Your data.xml file will look like the following:

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

After installing the module, these functions will be triggered automatically.

To read more about How to Invoke Functions from XML Files in Odoo 17, refer to our blog How to Invoke Functions from XML Files in Odoo 17.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
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