jieter / django-tables2

django-tables2 - An app for creating HTML tables
https://django-tables2.readthedocs.io/en/latest/
Other
1.87k stars 426 forks source link

Cannot set delimiter on CSV export #899

Open mjmare opened 1 year ago

mjmare commented 1 year ago

It seems that TableExport's parameter data_kwargs is not honoured when exporting a table.

I had to create this workaround in order to be able to set the delimiter:

class TableExportWithDelimiter(TableExport):

    def export(self):
        """
        Overide to provide a non standard delimiter. data_kwargs does not seem to work.
        """
        return self.dataset.export(self.format, delimiter=';')
jieter commented 1 year ago

dataset_kwargs is passed as keyword arguments to the Dataset:

https://github.com/jieter/django-tables2/blob/83ffbcd214f957fc63e4295a26e14b1944f0da66/django_tables2/export/export.py#L64

tablib does not support setting the export format on the Dataset, which makes sense as that would couple it to a certain export format (CSV).

I suspected your example would only work to export to CSV, because the xlsx format does not support the delimiter kwarg, but apparently, unsupported kwargs are ignored.

We could fix this by adding another dict on ExportMixin which is passed to dataset.export(), but I'd say your custom TableExport class already looks like a good fix for non-standard use cases?

mjmare commented 1 year ago

My example is CSV only indeed. My workaround works but overriding a class just to provide the delimiter kwart felt a bit heavy handed.