5

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 函数。

4

2 回答 2

6

在 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 }
于 2017-12-08T09:22:36.960 回答
0
@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

        });
    },
});
于 2018-04-18T09:40:05.573 回答