Odoo 13 中 one2many 字段列的导出数据验证

Validation on export data for one2many field column in Odoo 13

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

我的代码如下:

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'

导出时显示此验证

提前致谢。

您需要添加第二个条件,从相关模型中读取记录,以检查是否有处于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后面的代码是一样的,我只是简单的举了个例子。