验证Odoo 13中one2many字段列的导出数据 [英] Validation on export data for one2many field column in Odoo 13

查看:280
本文介绍了验证Odoo 13中one2many字段列的导出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show validation error when the user export records for state='draft'(in one2many field). I have done code for it and it's working fine. but when I put this code for one2many table then I unable to get a validation message.

我的代码如下:

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'

when I export record from 'transaction.log' for 'Draft' log_status then it's work and shows validation message. But I also want to show this validation when export from 'daily.transaction'

提前谢谢.

推荐答案

您需要添加第二个条件,并从相关模型中读取记录,以检查是否有某些记录处于Draft状态.

You need to add a second condition and read records from the related model to check if there is some record in Draft state.

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之后的代码是相同的,我只是举了一个简单的例子.

The code after then is the same, I just made a quick example.

这篇关于验证Odoo 13中one2many字段列的导出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆