2

当用户导出 state='draft'(在 one2many 字段中)的记录时,我想显示验证错误。我已经为它完成了代码,它工作正常。但是当我将此代码用于 one2many 表时,我无法获得验证消息。

我的代码如下:

class DailyTransaction(models.Model):
    _name = 'daily.transaction'
    _rec_name = 'batch_id'

    date = fields.Date()
    batch_id = fields.Char()
    daily_transaction = fields.One2many('transaction.log', 'daily_trans_log', string='Daily Transaction')


class Transaction_log(models.Model):
    _name = 'transaction.log'
    _rec_name = 'daily_trans_log'

    daily_trans_log = fields.Many2one('daily.transaction')
    log_status = fields.Selection([('Draft', 'Draft'), ('Approved', 'Approved'), ('Confirmed', 'Confirmed')],
                                  default='Draft', string='Log Status')


odoo.define("transaction_log.export_log", function(require) {
"use strict";

    var listController = require("web.ListController");
    var dialog = require("web.Dialog");

    listController.include({
         /**
         * Opens the Export Dialog
         *
         * @private
         */
        _onExportData: function () {
            var self = this;
            var do_export = true;
            // Avoid calling `read` when `state` field is not available
            if (self.initialState.fields.hasOwnProperty('log_status')) {
                self._rpc({
                    model: self.modelName,
                    method: 'read',
                    args: [self.getSelectedIds(), ['log_status']],
                }).then(function (result) {
                    // Check if we have at least one draft record
                    for(var index in result) {
                        var item = result[index];
                        if (item.log_status === 'Draft') {
                            do_export = false;
                            break;
                        }
                    }
                    if (do_export) {
                        self._getExportDialogWidget().open();
                    } else {
                        dialog.alert(self, "You can't export draft stage data!", {});
                    }
                });
            } else {
                this._getExportDialogWidget().open();
            }
        },
    });

});

当我从 'transaction.log' 为 'Draft' log_status 导出记录时,它可以工作并显示验证消息。但我也想在从“daily.transaction”导出时显示此验证

提前致谢。

4

1 回答 1

1

您需要添加第二个条件并从相关模型中读取记录以检查Draft状态中是否存在某些记录。

else if (self.initialState.fields.hasOwnProperty('daily_transaction')){
            self._rpc({
                model: 'transaction.log',
                method: 'search_read',
                args: [[['daily_trans_log', 'in', self.getSelectedIds()]], ['log_status']],
            }).then(function (result) {
                // Check if we have at least one draft record
                for(var index in result) {
                    var item = result[index];
                    if (item.log_status === 'Draft') {
                        do_export = false;
                        break;
                    }
                }
                if (do_export) {
                    self._getExportDialogWidget().open();
                } else {
                    dialog.alert(self, "You can't export draft stage data!", {});
                }
            });
        }

后面的代码then是一样的,我只是做了一个简单的例子。

于 2020-02-09T15:24:28.920 回答