简体   繁体   English

如何在odoo 11中的jQuery中调用python函数

[英]How to call python function in jquery in odoo 11

Python code: Python代码:

@api.model
def test_method(self):
    a= 10
    b = 20
    c = a+b
    return c

jQuery: 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));
  });
});

Error: 错误:

Missing depends, 缺少取决于

The above code will is working in odoo 10 but not in odoo 11. I want to know how to call a python function from JS. 上面的代码将在odoo 10中工作,但在odoo 11中不工作。我想知道如何从JS调用python函数。

in Odoo 11 you need to use 在Odoo 11中,您需要使用

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

instead of 代替

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

And later call a method using .query() method as below: 然后使用.query()方法调用一个方法,如下所示:

rpc.query({
            model: 'model.name',
            method: 'method_name',
            args: [{
                'arg1': value1,
                'arg2': value2,
            }]
        }).then(function (returned_value) { // do something }
@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

        });
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM