Enable Dark Mode!
invoking-functions-from-xml-files-in-odoo-17.jpg
By: Sigha CK

Invoking Functions From Xml Files in Odoo 17

Technical Odoo 17

With Odoo, managing any kind of data is easy. Any type of data can be created using XML files. On occasion, nevertheless, it will be necessary to install a module and perform an action in place of making a record on a model. In this blog, let's examine calling a method during module installation.

There are two methods for calling a function in XML.

1. Making use of a function with no arguments

2. Making an argument-based function call

I'll be using an XML file to invoke a method to create a new product in this blog post.

Let's first establish a group in Odoo in an XML file within the path. I'll then develop a module with the following files and folders.

Invoking Functions From Xml Files in Odoo 17-cybrosys

data/product_data.xml.

1. Using a function without any parameters

Within this method, it's possible to call a function encapsulated within any model without the necessity of passing parameters.. The syntax is given 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 is a container for defining data records or operations. 

The <data> tag serves as a container for specifying data records or processes, and when the noupdate attribute is set to 1, it indicates that any existing data matching the definitions in this file should remain unchanged in the database and not undergo updates.

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

Inside the <data> tag, there is a <function> element. This particular element denotes an Odoo function call, specifically referencing the new_product function. The model attribute points to the Odoo model, in this instance, product.template, on which the function is intended to operate. Upon execution, the function will carry out a distinct action associated with the creation of a new product within the Odoo database.

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

I have created a python file,product_template.py inside the directory models

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 establishes a fresh Python class named ProductTemplate, which inherits characteristics from the models.Model class supplied by Odoo. The purpose of this class is to serve as a customized model for overseeing and organizing information related to products or contacts.

_inherit = "product.template"

The use of the _inherit attribute indicates that the custom model is building upon the base model, product.template, inheriting its fields and functionalities while also allowing for the introduction or alteration of specific behaviors.

@api.model

def my_productself):

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

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

Within the func_without_params method, the following line utilizes the create method inherited from the base model to generate a new product entry in the database. The product record is instantiated with the name 'My product' by providing a dictionary containing the necessary field values.

Invoking Functions From Xml Files in Odoo 17-cybrosys

2. Using a function with parameters

Within this method, invoking a function defined within any model is achieved by passing the necessary parameters.

product_data.xml

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

We have a "value" tag with the value "Calculator" within the "function" element. The func_without_params function receives this value either as an argument or directly as a provided value during its execution.

product_template.py

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

One name for a parameter is required by the func_with_params method. Utilizing the create method not only creates new entries to the product.template table but also employs the provided name argument to generate the corresponding record.

Invoking Functions From Xml Files in Odoo 17-cybrosys

By following this approach, you can trigger the execution of a function 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
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