How to call python function in jquery in odoo 11

Python Code:

@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));
  });
});

Error:

Missing depends

The above code will work in odoo 10 but not in odoo 11. I want to know how to call a python function from JS.

+4
source share
2 answers

in Odoo 11 you need to use

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

instead

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

And later call the method using the .query () method, as shown below:

rpc.query({
            model: 'model.name',
            method: 'method_name',
            args: [{
                'arg1': value1,
                'arg2': value2,
            }]
        }).then(function (returned_value) { // do something }
+2
source
@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: [{'a':1,'b':20,}],//Wrong way to give parameters.
            args: [], //Right way.. Now gives the value of c.

        }).then(function(res){
            console.log(res);   //it gives object

        });
    },
});
0
source

Source: https://habr.com/ru/post/1688986/


All Articles