2

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.

2 Answers 2

1

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]);
             }

        },);
Sign up to request clarification or add additional context in comments.

Comments

0

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]);
              });
    },
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.