Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is working in odoo 10 but not in odoo 11. I want to know how to call a python function from JS.

like image 978
anand raj Avatar asked Nov 09 '17 05:11

anand raj


1 Answers

in Odoo 11 you need to use

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

instead of

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

And later call a method using .query() method as below:

rpc.query({
            model: 'model.name',
            method: 'method_name',
            args: [{
                'arg1': value1,
                'arg2': value2,
            }]
        }).then(function (returned_value) { // do something }
like image 65
dorsaf dhouibi Avatar answered Oct 23 '22 14:10

dorsaf dhouibi