Validation on export data for one2many field column in Odoo 13

非 Y 不嫁゛ 提交于 2020-04-15 22:41:28

问题


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.

My code is below:

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

});

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'

Thanks in advance.


回答1:


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!", {});
                }
            });
        }

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



来源:https://stackoverflow.com/questions/60111616/validation-on-export-data-for-one2many-field-column-in-odoo-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!