Skip to Content
Menu
This question has been flagged

I want to call python function in java script. I tried but it's not working.

Here is the my  PY code: 

class CoustomModel(models.Model):
_name = 'custom.model'

new_field = fields.Char(string="test", required=False, )


@api.model
def my_function(self):
print("Hello Word");


Here is the JavaScript
odoo.define('visio_learn_javascript.SampleWork', function (require) {
"use strict";

var rpc = require("web.rpc")
var Model = require('web.Model');

var custom_model = new Model('custom.model');
custom_model.call('my_function');

});
Avatar
Discard
Best Answer

Hi,

To call a Python function in JavaScript, I suggest you to use JSONRPC (see the example in the below code)

ajax.jsonRpc('/web/dataset/call_kw', 'call', {
'model': 'custom.model',
'method': 'my_function',
'args': [],
'kwargs': {
'context': {},
}
}).then(function (data) {
// Do something here
});

Best regards!

Avatar
Discard
Author

This is not working for me.

how about calling a python function from controllers in Javascript?

Can you give us examples?

Thanks

Related Posts Replies Views Activity
1
Dec 22
2711
0
Dec 20
50
0
Dec 15
12183
2
Aug 24
5004
1
Oct 22
22089