跳至內容
選單
此問題已被標幟
1 回覆
9352 瀏覽次數

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');

});
頭像
捨棄
最佳答案

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!

頭像
捨棄
作者

This is not working for me.

how about calling a python function from controllers in Javascript?

Can you give us examples?

Thanks

相關帖文 回覆 瀏覽次數 活動
1
12月 22
3698
0
12月 20
50
0
12月 15
13340
2
8月 24
6638
1
10月 22
24325