django-tables2 icon indicating copy to clipboard operation
django-tables2 copied to clipboard

Cannot set delimiter on CSV export

Open mjmare opened this issue 2 years ago • 2 comments

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=';')

mjmare avatar Mar 02 '23 15:03 mjmare

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?

jieter avatar Mar 06 '23 07:03 jieter

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

mjmare avatar Mar 06 '23 09:03 mjmare