2

I want to show validation on the 'draft' stage that the user can not export draft stage data. I know about 'def export_data(self, fields_to_export)' function but it works after select fields. I want that validation just when clicking on export action. So, anyone can suggest me which function I used for my requirement. I am using Odoo 13.

Thanks in advance.

Pawan Kumar Sharma
  • 1,168
  • 8
  • 30

2 Answers2

3

A way to do that is to override _onExportData of ListController.

Check the following code (It uses state field):

odoo.define("stack_overflow", 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('state')) {
                self._rpc({
                    model: self.modelName,
                    method: 'read',
                    args: [self.getSelectedIds(), ['state']],
                }).then(function (result) {
                    // Check if we have at least one draft record
                    for(var index in result) {
                        var item = result[index];
                        if (item.state === '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();
            }
        },
    });

});
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Kenly, Can you give me a solution that, how to call this function when my 'state' field in one2many? because for view its working well but my 'state' in one2may field. – Pawan Kumar Sharma Feb 07 '20 at 07:20
  • here is my exact problem link: https://stackoverflow.com/questions/60111616/validation-on-export-data-for-one2many-field-column-in-odoo-13 – Pawan Kumar Sharma Feb 07 '20 at 11:13
0

You can use this method def view_init(self, fields).In this, you can add your Validation.

Thanks

Dipen Shah
  • 2,396
  • 2
  • 9
  • 21