- 上传者: Administrator
- 上传时间:2023年11月19日 02时32分33秒
* Function Call from XML ** *** There may be some cases where you will need to perform some functions or methods once a module is installed. It is p ......
Function Call from XML
There may be some cases where you will need to perform some functions or methods once a module is installed. It is possible in Odoo to invoke a function from XML. The <function> tag is used to run python functions from XML. There are two ways to invoke a function from XML.
1. Invoking function without parameters
2. Invoking function with parameters
Invoking function without parameters
In this method, we can call a function defined inside any model without passing parameters. The syntax is given below.
<function id="function_id" model="model_name" name="method_name"/>
The model of the method is specified in the attribute model of the function tag. The other attributes are id and name. Id specifies the unique identifier of the XML record, and the name corresponds to the method name.
Let us see an example of a function call without parameters.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<function id="func_call_without_params" model="product.product"
name="func_without_params"/>
</data>
</odoo>
Here the model is product.product. So the system will invoke the method named func_call_without_params defined inside the model product.product. So the method definition will be as follows.
from odoo import api, fields, models
class product(models.Model):
_inherit = "product.product"
@api.model
def func_without_params(self):
self.create(dict(name='Test'))
After adding the XML data file inside the manifest, the method will be called on installing the module, and a new record will be created inside the model product.product.
Invoking function with parameters
The next method is Invoking function with parameters. In this method, we can call a function defined inside any model with passing parameters. The syntax is somewhat different from the previous one, and it is given below.
<function id="function_id"
model="model_name"
name="method_name">
<value>param1</value>
<value>param2</value>
<!-- pass the parameters as per your need-->
</function>
Parameters for the method are passed inside value tag. It is possible to pass as many parameters as per your need. These parameters can be accessed from the method inside the model. An example is given below.
<odoo>
<data noupdate="1">
<function id="func_call_with_params"
model="product.product"
name="func_with_params">
<value>Cybrosys Technologies</value>
</function>
</data>
</odoo>
Then the method definition need to be as follows.
from odoo import api, fields, models
class product(models.Model):
_inherit = "product.product"
@api.model
def func_with_params(self, name):
self.create(dict(name=name))
Then add the XML file inside the manifest. On installing the custom module, the python function will be invoked. The value of the parameter will be fetched, and this will be used to create the new product record.
本文章从网上收集,如有侵权请联系tderp@tderp.com删除
- 微信扫一扫,一分也是爱:
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。