user_123
user_123

Reputation: 450

How to call python function in odoo11 js file?

How to call the python function from js file. I used the following code but i didn't work. Here, i want to show message in js file which is define in .py file. .py file from odoo import models, fields, api

class message_of_the_day(models.Model):
    _name = "oepetstore.message_of_the_day"

    @api.model
    def my_method(self):
        return {"hello": "world"}

    message = fields.Text()
    color = fields.Char(size=20)

.js file

odoo.define('petstore.petstore', function (require) {
"use strict";

    var Widget = require('web.Widget');
    var core = require('web.core');
    var web_client = require('web.web_client');
    var AbstractAction = require('web.AbstractAction');
    var ControlPanelMixin = require('web.ControlPanelMixin');

    var MessageOfTheDay = Widget.extend({
        template: "MessageOfTheDay",
        start: function() {
            var self = this;
            return new instance.web.Model("oepetstore.message_of_the_day")
                .query(["message"])
                .order_by('-create_date', '-id')
                .first()
                .then(function(result) {
                    self.$(".oe_mywidget_message_of_the_day").text(result.message);
                });
        },
    });

    var HomePage = AbstractAction.extend(ControlPanelMixin.{
        template: "HomePage",
        start: function() {
            var messageofday = new MessageOfTheDay(this)
            messageofday.appendTo(this.$el);
        },
    });
    core.action_registry.add('message.homepage', HomePage);

})

;

I tried to solve the exerciser https://www.odoo.com/documentation/11.0/howtos/web.html#exercises using odoo11 js.

Upvotes: 2

Views: 2129

Answers (2)

Anitha Das B
Anitha Das B

Reputation: 372

Please use the below code for looping the result

        rpc.query({
            model: 'model_name',
            method: 'method_name',
            args: [arg_fields],
        })
        .then(function(result){ 
            for (i= 0; i< result.length; i++){
                    console.log("result",result[i]);
             }

        },);

Upvotes: 1

Tamir Tsedev
Tamir Tsedev

Reputation: 1

Try this

var rpc = require('web.rpc');
var MessageOfTheDay = Widget.extend({
    template: "MessageOfTheDay",
    start: function() {
        rpc.query({
              // your model 
              model: message_of_the_day,
              //read data or another function
              method: 'my_method',
              //args, first id of record, and array another args
              args: [],

             })
             .then(function(result){
                //your code when data read
               self.$(".oe_mywidget_message_of_the_day").text(result[0]);
              });
    },
});

Upvotes: 0

Related Questions