Navi
Navi

Reputation: 1052

How to give import action to a button in odoo?

I want to give the import/export action to a custom button in odoo 10, in csv file or excel file. How to do it?

Thank You

Upvotes: 0

Views: 1470

Answers (1)

jo541
jo541

Reputation: 1155

The method calling when you click on the button export is export_data

You can see the code in the file models.py in folder /odoo/odoo/models.py

@api.multi
def export_data(self, fields_to_export, raw_data=False):
    """ Export fields for selected objects

        :param fields_to_export: list of fields
        :param raw_data: True to return value in native Python type
        :rtype: dictionary with a *datas* matrix

        This method is used when exporting data via client menu
    """
    fields_to_export = map(fix_import_export_id_paths, fields_to_export)
    if raw_data:
        self = self.with_context(export_raw_data=True)
    return {'datas': self._export_rows(fields_to_export)}

I don't try but I think you must do like this

@api.multi
def execute(self):
    model = "name_of_your_model_want_export"
    export_datas = self.env[model].export_data(['field1', 'field2'])
    # code to write to a file CSV of EXCEL

For the import. You must do use the create method of the model.

@api.multi
def execute(self):
    # This function return list of dict like this 
    # [{'field1': 'Value1','field2': 'value2}, {'field1': 'Value1','field2': 'value2}]
    datas = data_from_your_csv(self.file) 
    model = "name_of_your_model_want_import"
    for data in datas:
        record = self.env[model].create(data)

Upvotes: 2

Related Questions