简体   繁体   中英

how to call python method from odoo 9 javascript

I have a piece of code to call the python method in javascript , but the code did not work , and an error will be show .ie

 error: Some modules could not be started 
 Failed modules: ["group_js.costume"] 
 Debug:  Object {group_js.costume: Object}

and my .py file is

class group_js(osv.osv):
  _name = "group_js"
  _description = "Group JS"
  _columns={
      'js' : fields.text('Javascript',required = True , store=True),
     }
 @api.multi
 def get_record(self):
     return [(i.js) for i in self]

my .js file is

odoo.define('group_js.costume', function (instance) {
     new instance.web.Model("group_js").call("get_record",[list_of_record]).then(function(result){
     console.log("hiiii");
   });
});

so i have no idea how to fix that , so please help me for fix this error .

Thanks

To call a method from the odoo python model you have Model().call() in javascript.

For example think of following model in py file.

class group_js(models.Model):
   _name = "model.example"
   _description = "Example model"

   def get_record(self):
       return ''

And from the javascript file you can call it as:

new Model("model.example").call("get_record",[args]).then(function(result){ 
    console.log(result);//show in console Hello
});

Here the further details of call method:

/**
 * Call a method (over RPC) on the bound OpenERP model.
 *
 * @param {String} method name of the method to call
 * @param {Array} [args] positional arguments
 * @param {Object} [kwargs] keyword arguments
 * @param {Object} [options] additional options for the rpc() method
 * @returns {jQuery.Deferred<>} call result
 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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