Export/mport data UNIVERSAL function

Good evening,

Definitely, everything is in docs, but may you tell me like I am five:
how do the export data from an arbitrary model?
Inputs:
items = Transaction().context['active_ids'] # list of selected items ids
Outputs:
a CSV format with all fields (including M2M, O2M)

I build already a pretty complex algorithm if recognizing fields names and it values:

    def populate_csv_data(self, input_model_instance,keys_only=False):
        # ''' Returns one copy of class by a call by extracting input_model_instance
        #   input_model_instance — a tryton Model
        #   keys_only — boolean — returns keys' names instead of their values
        _export_data = [] 
        _accepted_keys_types = ['char','text','numeric','integer','boolean','timestamp','float','datetime','selection','many2many','many2one'];
        ########## THIS IS THE ESSENCE OF THE QUESTION #####################
        _fields = input_model_instance.fields_get(level=2)
        #### WOULD YOU RECOMMEND USE ANOTHER CALL??? 
        ######## ModelStorage.export_data ??????
        if keys_only: # return a CSV line with keys only 
            _export_data.append('id#')
            _export_data = _export_data + list(_fields)
            _export_data.append('\r\n') # return data with ending '\r\n'
            return _export_data
        for _key in list(_fields): 
            #_export_data.append(_key)
            try:
            # ———— Convert various types into CSV coded strings
                if _fields[_key]['type'] in _accepted_keys_types: #,
                    key_value=getattr(vehicle,_key)
                    if key_value:
                        if _fields[_key]['type'] == 'timestamp':
                            _export_data.append('{0:%Y-%m-%d %I:%M %p}'.format(key_value))
                        elif _fields[_key]['type'] == 'datetime':
                            _export_data.append('{0:%Y-%m-%d %I:%M %p}'.format(key_value))
                        elif _fields[_key]['type'] == 'numeric':
                            _export_data.append('{0:0.2f}'.format(key_value))
                        elif _fields[_key]['type'] == 'integer':
                            _export_data.append('{0:.0f}'.format(key_value))
                        elif _fields[_key]['type'] == 'text': # remove carriage returns from lines
                            _export_data.append('"{}"'.format(key_value))
                        elif _fields[_key]['type'] == 'char': # remove carriage returns from lines
                            _export_data.append('"{}"'.format(getattr(vehicle,_key)))
                        elif _fields[_key]['type'] == 'many2many':
                            _export_data.append('"{}"'.format(key_value))
                        elif _fields[_key]['type'] == 'many2one':
                            _export_data.append('"{}"'.format(key_value.name))
                        # —— All other fields from this list
                        else:
                            _export_data.append(key_value)
                    else:
                        _export_data.append(''); # Add emplty if data is empty (None type)
                else:
                    _export_data.append(''); # SKIP cells of not permitted types
            # ———— Completed conversion various types int CSV coded strings
            except Exception as e:
                _export_data.append("{}".format(e));
        _export_data.append('\r\n') # return data with ending '\r\n' 
        return _export_data

How to get data from the model don’t knowing the keys?
How you would recommend to manage this task?

Constantine.

Why do not you use the CSV export from the client?

I want to add some extra logic to the data conversion. Also, I want to create a “ONE BIG BUTTON” solution for the client.

The ModelStorage.export_data can be customized if needed.
Or probably better is to add new fields (Function probably) that expose as a schema the special data format.
But the format used by Tryton is pretty standard.

Or if it is a special text file for a specific Model, you can define a Report for it.

Either proposal is good!
Will create a report. The idea with having a hidden field with a schema — a great proposal!
————
Where I can find an example of ModelStorage.export_data usage?
Didn’t find in standard modules.

There are several examples in the export data tests :wink: