Calling Functions From XML Files in Odoo 17

With Odoo, handling various types of data becomes effortless. XML files allow for the creation of diverse data types. However, there may be instances where installing a module and executing an action is preferable to simply creating a record in a model. In this blog, we'll explore invoking a method during module installation.

There are two approaches for invoking a function in XML.

1. Utilizing a function without any arguments

2. Invoking a function with arguments

In this blog post, I'll demonstrate how to utilize an XML file to invoke a method for creating a new product.

First, we'll define a group in Odoo within an XML file located at the specified path. Then, I'll create a module with the subsequent files and directories.


data/product_data.xml

1. Employing a function with no parameters

In this method, you can invoke a function encapsulated within any model without needing to pass parameters. The syntax is provided below.

product_data.xml

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

The <data> tag serves as a container for defining data records or operations.

The <data> tag functions as a container for defining data records or processes. When the noupdate attribute is set to 1, it signifies that any existing data matching the definitions in this file should remain unaltered in the database and not be updated.

<function model="product.template" name="new_product"/>

Within the <data> tag, there exists a <function> element. This element signifies an Odoo function call, specifically referring to the new_product function. The model attribute indicates the Odoo model, in this case, product.template, on which the function is designed to operate. Upon execution, the function will perform a specific action related to creating a new product in the Odoo database.

To comprehend the implementation of the function, let's examine its corresponding code in the Python file.

I've generated a Python file named https://product_template.py/ within the models directory.

product_template.py

from odoo import models, fields, api
class ProductTemplate(models.Model):
  _inherit = "product.template"
  @api.model
  def my_product(self):     
​ self.create(dict(name='My Product'))

class ProductTemplate(models.Model):

This line defines a new Python class called ProductTemplate, inheriting properties from the models.Model class provided by Odoo. This class is designed to function as a customized model for managing and structuring data associated with products or contacts.

_inherit = "product.template"

By employing the _inherit attribute, it signifies that the custom model extends the base model, product.template, inheriting its fields and functionalities. This approach also permits the introduction or modification of particular behaviors.

@api.model

def my_product(self):

The @api.model decorator is utilized to specify a model-level method in Odoo. The func_without_params method exclusively accepts the standard self parameter, which refers to the current instance of the model, without any additional parameters.

self.create(dict(name='My product'))

Inside the func_without_params function, the subsequent line employs the create method inherited from the base model to produce a new product entry in the database. The product entry is initialized with the name 'My product' by supplying a dictionary containing the required field values.


2. Employing a function with arguments

In this approach, calling a function defined within any model is accomplished by supplying the required parameters.

data file for products

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

Within the "function" element, there's a "value" tag set to "Calculator." The func_without_params function can acquire this value either as an argument or directly as a supplied value during its execution.

https://template_product.py/​

class ProductTemplate(models.Model):
  _inherit = "product.template"
  @api.model
  def func_with_params(self, name):     
self.create(dict(name=name))

The func_with_params method necessitates at least one parameter name. By utilizing the create method, not only are new entries created in the product.template table, but the provided name argument is also used to generate the corresponding record.

Creating an XLSX Report with a Controller in Odoo 17