簡體   English   中英

如何在odoo11 js文件中調用python函數?

[英]How to call python function in odoo11 js file?

如何從js文件中調用python函數。 我使用了以下代碼,但是我沒有工作。 在這里,我想在.py文件中定義的js文件中顯示消息。 odoo的.py文件導入模型,字段,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

嘗試這個

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

請使用以下代碼循環結果

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

        },);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM