In the post, i will briefly tell about how to call methods while module installation using XML <function /> tags in Odoo.
Sometime we required to perform some CURD operation automatically just after module installation , in this case we can use the <function/> tags and call the method to perform these operations.
Create DEMO DATA File
First of all you need to create a xml file(let’s say demo.xml) Like:
<?xml version="1.0" encoding="utf-8"?> <odoo> <data noupdate="1"> </data> </odoo>
Here noupdate is set to true for ensuring that on module update this xml will not executed again and again.
It’s time to include your xml file in data or demo key of __mainifest__.py.
Calling Method Without parameters
Create a method in model Like:
from odoo import api, fields, models class product(models.Model): _inherit = "product.product" @api.model def oe_method_without_params(self): self.create(dict(name='demo.webkul.com'))
Now for calling this method , create a <function/> tag in demo.xml Like:
<function id="function_id" model="model_name" name="method_name"/>
<function id="function_without_params" model="product.product" name="oe_method_without_params"/>
Now your demo.xml file will look like:
<odoo> <data noupdate="1"> <function id="function_without_params" model="product.product" name="oe_method_without_params"/> </data> </odoo>
Once module is installed this method(oe_method_without_params
) will called.
Calling Method With parameters
Include one more method in you python file:
@api.model def oe_method_with_params(self,name,sale_ok=True): self.create(dict(name=name,sale_ok=sale_ok))
You python file will look like:
from odoo import api, fields, models class product(models.Model): _inherit = "product.product" @api.model def oe_method_without_params(self): self.create(dict(name='demo.webkul.com')) @api.model def oe_method_with_params(self,name,sale_ok=True): self.create(dict(name=name,sale_ok=sale_ok))
Now for calling this method , create a <function/> tag and pass the params in <value/> tags Like:
<function id="function_id" model="model_name" name="method_name"> <value>param1</value> <value>param2</value> .... </function>
<function id="function_with_params" model="product.product" name="oe_method_with_params"> <value>erp.webkul.com</value> <value>False</value> </function>
Now your demo.xml file will look like:
<odoo> <data noupdate="1"> <function id="function_without_params" model="product.product" name="oe_method_without_params"/> <function id="function_with_params" model="product.product" name="oe_method_with_params"> <value>erp.webkul.com</value> <value>False</value> </function> </data> </odoo>
Once module is installed these method will called.
Hope you enjoyed this post, I’d be very grateful if you’d write your opinions, comments and suggestions to keep the page updated and interesting.
You also like our post on working with xml data in odoo.
how do i create dependent input selection fields at website forms in odoo 12.
please tell me .