Skip to Content
Menu
This question has been flagged
4 Replies
8411 Views

hi how call a function python in file javascript in odoo 15 please help


Avatar
Discard
Best Answer

Hi,

It is not possible to call a Python function from JavaScript in Odoo 15 directly, as they are two separate programming languages and run on different environments (Python on the server side, JavaScript on the client side). However, you can achieve this by making an API call to the server, where the Python function is located, from your JavaScript code. In Odoo 15, this is typically done using the 'rpc' method provided by the odoo.define JavaScript method.

Here is an example of how you can call a Python function named 'my_python_function' that is defined in an Odoo model named 'my.model' from JavaScript in an Odoo 15:

odoo.define('my_module', function (require) {
var rpc = require('web.rpc');

// Call the Python function
rpc.query({
model: 'my.model',
method: 'my_python_function',
args: [arg1, arg2, ...],
}).then(function (result) {
console.log(result);
});
});

You can pass any arguments you want to the Python function by including them in the 'args' array.
The result of python function will be returned as a promise and you can use the then function to catch it.

Regards

Avatar
Discard
Best Answer

Hello when i try to call function using this._rpc on the js file, i got error "TypeError: this._rpc is not a function", can someone help me ?

Avatar
Discard
Author Best Answer

My function python call a action view how and i need every 1h show this action

Avatar
Discard

You can use the Python built-in library schedule to schedule the execution of your function that calls the action view every hour. Here's an example:

Copy code
import schedule
import time

def call_action_view():
# Code to call the action view goes here

schedule.every(1).hour.do(call_action_view)

while True:
schedule.run_pending()
time.sleep(1)
This code will run the call_action_view function every hour indefinitely.

In Odoo, you can also schedule the execution of a function using the ir.cron model. You would need to create a new record in the ir.cron model with the following fields:

name: A name for the cron job
model_id: The model where the function is located
function: The name of the function you want to execute
interval_number: The frequency at which the function should be executed (e.g. 1 for every hour)
interval_type: The unit of time for the frequency (e.g. 'hours')
nextcall: The next time the cron job will be run
You can also use another odoo module that is odoo-automation it will help you to schedule the function you want to execute and also you can use it to execute any odoo functionality as well.

Best Answer

If you need to call a Python function from JavaScript in Odoo 15, you can use the Odoo's RPC (Remote Procedure Call) mechanism to call the Python function on the server and get the result back to the browser.


You can use the odoo.jsonRpc() method provided by the Odoo JavaScript API to call a Python method. The first parameter of this method is the name of the method to call, the second parameter is an object containing the method's parameters, the third parameter is a callback function that will be called when the server returns a response.


For example, if you have a Python method named my_python_method that you want to call from JavaScript, you can use the following code:



odoo.jsonRpc("/your/controller/path", 'call', {

    'method': 'my_python_method',

    'params': {'param1': 'value1', 'param2': 'value2'}

}).then(function (result) {

    // do something with the result here

});

Please note that you need to specify the correct path to your controller, and also need to make sure that you have a corresponding method defined in your controller.

Avatar
Discard