Python代码:
@api.model
def test_method(self):
a= 10
b = 20
c = a+b
return c
jQuery:
var Model = require('web.Model');
$(document).ready(function() {
var test_model = new Model("MyClass");
test_model.call("test_method").then(function(c) {
console.log("res ult:" + JSON.stringify(result));
});
});
错误:
的丢失取决于
上面的代码可以在odoo 10中运行,但不能在odoo 11中运行。我想知道如何从JS调用python函数。
发布于 2017-12-08 17:22:37
在Odoo 11中,您需要使用
var rpc = require('web.rpc');
而不是
var Model = require('web.Model');
然后使用.query()方法调用方法,如下所示:
rpc.query({
model: 'model.name',
method: 'method_name',
args: [{
'arg1': value1,
'arg2': value2,
}]
}).then(function (returned_value) { // do something }
发布于 2018-04-18 17:40:06
@api.model
def my_method(self):
a= 10
b = 20
c = a+b
return c
var MessageOfTheDay = Widget.extend({
template: "MessageOfTheDay",
start: function() {
var self = this;
this._rpc({
model: 'pettoys.pettoys',
method: 'my_method',
args: [], //Now gives the value of c.
}).then(function(res){
console.log(res); //it gives object
});
},
});
https://stackoverflow.com/questions/47194770
复制相似问题