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

Invoking Functions from XML Files in Odoo 16

Technical Odoo 16

Any type of data may be simply managed in Odoo. XML files can be used to create any kind of data. However, there are times when it will be required to install a module without creating a record on a model and instead conduct an action. Let's look into calling a method while installing a module in this blog.

There are two ways to use XML to call a function.

1. Using a function without any parameters

2. Calling a function with arguments

In this blog, I’m going to create a new partner from a method called from an XML file.

First, let’s create a group in Odoo in an XML file inside the path, and I'm going to create a module here with the following files and folders:

Invoking Functions from XML Files in Odoo 16-cybrosys

data/partner_data.xml.

1. Using a function without any parameters

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

partner_data.xml

?xml version="1.0" encoding="utf-8"?>
   <odoo>
       <data noupdate="1">
           <function model="res.partner" name="new_partner"/>
       </data>
   </odoo>

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

A container for defining data records or processes is the <data> tag. The noupdate attribute is set to 1, which denotes that if the data defined in this file already exists in the database, it should not be updated.

<function model="res.partner" name="new_partner"/>

Inside the <data> tag, there is a <function> element. This element represents a function call in Odoo. In this case, the function specified is new_partner. The model attribute indicates the Odoo model on which the function will be executed, which is res.partner.

When this function is executed, it will perform a specific action related to creating a new partner or contact record in the Odoo database. 

Let’s check how the function is written in the Python file. I have created a python file,res_users.py inside the directory models

res_partner.py

class ResPartner(models.Model):
   _inherit = "res.partner"
   @api.model
   def new_partner(self):
       self.create(dict(name='Test partner'))

class ResPartner(models.Model):

This line defines a new Python class called ResPartner that inherits from the models.Model class provided by Odoo. This class represents a custom model for managing partner or contact information.

_inherit = "res.partner"

The _inherit attribute indicates that this custom model is inheriting from the base model res.partner. It means that the custom model will include all the fields and functionality of the base model and can add or modify behavior as needed.

@api.model

def new_partner(self):

The @api.model decorator is used to define a model-level method in Odoo. The func_without_params method does not take any additional parameters apart from the standard self parameter, which refers to the current instance of the model.

self.create(dict(name='Test partner'))

Inside the func_without_params method, this line creates a new partner record in the database using the create method inherited from the base model. It creates a partner record with the name 'Test partner' by passing a dictionary with the desired field values.

Invoking Functions from XML Files in Odoo 16-cybrosys

1. Using a function with parameters

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

partner_data.xml

<data noupdate="1">
<function model="res.partner" name="func_without_params">
   <value>Mars</value>
</function>
</data>

We have a "value" tag with the value "Mars" within the "function" element. This value is being supplied to the func_witho_params function as an argument or as a value.

res_partner.py

@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. Using the create method, it adds new records to the res.partner table. The name passed as an argument is used to produce the record.

Invoking Functions from XML Files in Odoo 16-cybrosys

This is how you can invoke a function from XML files in Odoo 16.


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