首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在odoo 11中调用jquery中的python函数

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

Stack Overflow用户
提问于 2017-11-09 13:52:13
回答 2查看 4.5K关注 0票数 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函数。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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 }
票数 6
EN

Stack Overflow用户

发布于 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

        });
    },
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47194770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档