如何在odoo11 js文件中调用python函数?

时间:2018-04-17 04:53:31

标签: javascript odoo-10 odoo-11

如何从js文件调用python函数。我使用以下代码,但我没有工作。在这里,我想在.s文件中定义的js文件中显示消息。 .py文件 来自odoo导入模型,字段,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文件

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

})

我尝试使用odoo11 js来解决锻炼者https://www.odoo.com/documentation/11.0/howtos/web.html#exercises

2 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

请使用以下代码循环显示结果

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

        },);