This question has been flagged
4 Replies
29786 Views

I am trying to call python function from javascript.

Python class:

class message_of_the_day(osv.osv):
   _name = "message_of_the_day"

   def my_method(self, cr, uid, context=None):
    return {"hello": "world")}

Javascript file:

openerp.oepetstore = function(instance) 
{
  instance.oepetstore.MyClass = instance.web.Class.extend(
  {
    hi_start: function() 
    {
      var self = this;
      var model = new instance.web.Model("message_of_the_day");
      model.call("my_method", [], {context: new instance.web.CompoundContext()}).then(function(result) 
      {
        console.log("hello world, I am working");
      });
  },
});
var my_object = new instance.oepetstore.MyClass();
my_object.hi_start();
}

When I run this module, the page stopped loading.

What am I doing wrong here?

Avatar
Discard
Author

Any help???

Best Answer

you can use call function or get_func in javascript to call a python function. for example in javascript new instance.web.Model(<model.name.in.quotes>).get_func(<yourfunction_name>)(<arguments>)

Avatar
Discard
Best Answer

you have to use the RPC:

ODOO Remote Procedure Calls

Use 'this', single quotes and the done function and it should work:

this._model = ...;
var x = this._model.call('my_method',[]).done(function(results){
  whatever;    
});
 

 

Avatar
Discard
Best Answer

Hi i have done with backend module .. calling odoo model from javascript . but when i try same on web ie fronted module its gives me error Referrence Error :Openerp is not define  ON => openerp.dummy = function(instance)

any idea?

 

Avatar
Discard

@Kashif. This is an old post and your inquiry is different from the original one. I would suggest that you create your own question and post your code there.

Best Answer

have you manage to get it working? i have tried this for POS, and it is possible to call python from js.

Avatar
Discard