Enable Dark Mode!
invoking-functions-from-xml-files-in-odoo-15.jpg
By: Arunima

Invoking Functions from XML Files in Odoo 15

Technical Odoo 15

In Odoo, we can easily manage any type of data. All types of data can be created using XML files. But in some cases, it will be necessary to perform an action rather than create a record on a model while installing a module. In this blog, let’s see how to call a method while installing a module.

In this blog, I’m going to create a group, and assign all the users existing in the system to this group.

First, let’s create a group in Odoo in an XML file inside the path security/security.xml.

security.xml

<odoo>
   <data noupdate="1">
       <record id="enable_user_permission" model="res.groups">
           <field name="name">Enable User Permission</field>
       </record>
   </data>
</odoo>

Here I have opened a data tag and used the parameter noupdate, it is indicating that Reimporting the values from the XML files should be done when a module is updated. The noupdate parameter makes sure that this XML is not repeatedly executed when the module is being updated.

Don’t forget to include this data file in your __manifest__.py file of the module.

We are going to assign all the existing users in the system to this particular group. So, let’s see how to invoke a function from this XML file. For this, we will use the function tag.

<function model="res.users" name="_default_group_get"/>

In the name attribute, we need to specify the function name, and in the model, we will specify the model where the function is written.

Now the entire XML file looks like this,

security.xml

<odoo>
   <data noupdate="1">
       <record id="enable_user_permission" model="res.groups">
           <field name="name">Enable User Permission</field>
       </record>
       <function model="res.users" name="_default_group_get"/>
   </data>
</odoo>

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_users.py

# -*- coding: utf-8 -*-
from odoo import api, fields, models, _
class ResUsers(models.Model):
   _inherit = 'res.users'
   @api.model
   def _default_group_get(self):
       group = self.env.ref(user_permission.enable_user_permission')
       users = self.search([])
       users.write({'groups_id': [(4, group.id)]})

We need to inherit the particular model and write the function inside the model.

Inside the group variable, we will get the particular group with the external id of the group.

In the users variable, we get all the existing users in the system as an object.

Then, Using the write method, we will assign the particular user to the group we have created.

Now, once we install the module, this function will be called.


invoking-functions-from-xml-files-in-odoo-15-cybrosys

This is how we can invoke a function from XML files in Odoo. Hope this blog was helpful.


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