# Export/mport data UNIVERSAL function

**URL:** https://discuss.tryton.org/t/export-mport-data-universal-function/3392
**Category:** Developer
**Created:** [October 24, 2020, 8:45pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392 "2020-10-24T20:45:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [October 24, 2020, 8:45pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/1 "2020-10-24T20:45:26Z")

</div>

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:

```auto
    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.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 24, 2020, 8:59pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/2 "2020-10-24T20:59:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [October 24, 2020, 9:01pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/3 "2020-10-24T21:01:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 24, 2020, 9:07pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/4 "2020-10-24T21:07:26Z")

</div>

> [@banknote](#):
>
> I want to add some extra logic to the data conversion

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.

---

<div class="post-metadata">

### Author: ![banknote](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/banknote/32/980_2.png) [@banknote](https://discuss.tryton.org/u/banknote)
#### Post date: [October 24, 2020, 9:18pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/5 "2020-10-24T21:18:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [October 24, 2020, 9:31pm UTC](https://discuss.tryton.org/t/export-mport-data-universal-function/3392/6 "2020-10-24T21:31:01Z")

</div>

> [@banknote](#):
>
> Where I can find an example of `ModelStorage.export_data` usage?

There are several examples in the [export data tests](https://hg.tryton.org/trytond/file/f6d3843711f6/trytond/tests/test_exportdata.py) 😉
