Odoo Help


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.


Web module : call python code from JS

on 4/17/13, 12:44 PM 3,355 views

I'm mew in OpenERP and I'm trying to create a web module "web_example".

  +-- __openerp__.py
  +-- __init__.py
  +-- web_example.py
  +-- web_example_view.xml
  +-- static/
       +-- src/
            +-- js/
            +-- xml/

In web_example.py I have a function that returns the content of a file on the server. In static/src/xml/web_exemple.py I have a button which triggers javascript from first_module.js. And in first_module.js I would like to call the function from web_example.py to get the content of the file. How should I do?

Here is my code :

// static/src/js/first_module.js    
openerp.web_example = function (instance) {
        instance.web.client_actions.add('example.action', 'instance.web_example.Action');
        instance.web_example.Action = instance.web.Widget.extend({
            template: 'web_example.action',
            events: {
                'click .oe_web_example_print button' : 'print_start',
                'click .oe_web_example_load button' : 'load_start'
            init: function () {
                this._super.apply(this, arguments);

            print_start: function () {
            load_start: function () {



from osv import fields, osv
import re

class web_example(osv.osv):

    #_table ='test_printer'
    _description = 'Test pour l\'impression de fichiers'

    def button_function(self, cr, uid, ids, context=None):
        filename = '/home/stage2013/Documents/testlecture'
        fichier = open('/home/stage2013/Documents/testlecture', 'r')
        toutesleslignes = fichier.readlines()
        return toutesleslignes  

Dharti Ratani (dhr)
On 7/4/13, 10:06 AM

To call a py method in openerp web_addon, first get the dataset od that object and then call the method. For ex, in this case it can be like :

load_start: function () {

//ids = get the id of the current record
   // context : get the context 
var ds = new instance.web.DataSet(this, 'web.example', context);
ds.call('button_function', [ids]).done(function(r) {
    //process the value returned from 'button_function' as per your requirement.


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)


Asked: 4/17/13, 12:44 PM
Seen: 3355 times
Last updated: 3/16/15, 8:10 AM