Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How do I call a python function from a JS file in Odoo v8?

By
Gopakumar N G
on 3/17/15, 1:45 AM 3,112 views

I have created a new module in Odoo v8 and created one template in it. For that template based on some values I need to call a python method from a JS file. I have tried the following methods:

1)

function openerp_pos_models(instance){

var myModel = new instance.web.Model('my.model');

});

but on loading the page it shows instance is not defined?

2) var Users = new openerp.web.Model('res.users');

this one shows Uncaught ReferenceError: openerp is not defined and Uncaught TypeError: Cannot read property 'Model' of undefined.


How can call a method from a JS file in odoo v8? The above methods are working in v7 and v8 base modules.

me too want to know.. this.

Anand
on 3/19/15, 2:25 AM

You should make Ajax /jQuery calls from the Javascript to the Python method. Have a look at http://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code this for example. Should be working in Odoo too.

Yenthe
on 3/20/15, 4:44 AM
0

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 3/24/15, 9:18 AM

Hi,

You need to define the instance for your custom module like this:

Here, "custom_module" is the folder name of your module. And here I have just created a widget. And you can call a python method using the call() function as shown in example. Just mention the name of method(eg: "action_test"), which is defined in your model. After method name, the second arguement is used pass some context if any.


For example:

in .py file


class custom_module(models.Model):

_inherit = "product.product"

@api.model

def my_method(self):

      return {"hello": "world"}


in .js file

openerp.custom_module = function(instance, local) {

      var _t = instance.web._t,

      _lt = instance.web._lt;

      var QWeb = instance.web.qweb;

      local.WidgetName = instance.Widget.extend({

 

              start: function () {

               this.$el.append(QWeb.render("YourTemplateName",{name: "Your name"})); // this is another way of selecting template and pass values in context to that template

              

                    var self = this;

                    var model = new instance.web.Model("product.product");

                    model.call("my_method", {context: new instance.web.CompoundContext()}).then(function(result) {

                    self.$el.append("<div>Hello " + result["hello"] + "</div>");

                                   });

          }

});

instance.web.client_actions.add('your.object', 'instance.custom_module.WidgetName');

It shows the following error: ""Uncaught TypeError: Cannot read property 'web' of undefined "" from the following line: return new instance.web.Model('product.product')

Gopakumar N G
on 4/13/15, 3:31 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/17/15, 1:45 AM
Seen: 3112 times
Last updated: 4/13/15, 10:21 PM