tabular_export.core module

Exports to tabular (2D) formats

This module contains functions which take (headers, rows) pairs and return HttpResponses with either XLSX or CSV downloads

The export_to_FORMAT_response functions accept a filename, and headers and rows. This allows full control over the data using non-database data-sources, the Django ORM’s various aggregations and optimization methods, generators for large responses, control over the column names, or post-processing using methods like get_FOO_display() to format the data for display.

The flatten_queryset utility used to generate lists from QuerySets intentionally does not attempt to handle foreign-key fields to avoid performance issues. If you need to include such data, prepare it in advance using whatever optimizations are possible and pass the data in directly.

If your Django settings module sets TABULAR_RESPONSE_DEBUG to True the data will be dumped as an HTML table and will not be delivered as a download.

tabular_export.core.convert_value_to_unicode(v)[source]

Return the UTF-8 bytestring representation of the provided value

date/datetime instances will be converted to ISO 8601 format None will be returned as an empty string

tabular_export.core.export_to_csv_response(filename, *args, **kwargs)[source]

Returns a downloadable StreamingHttpResponse using an CSV payload generated from headers and rows

tabular_export.core.export_to_debug_html_response(filename, headers, rows)[source]

Returns a downloadable StreamingHttpResponse using an HTML payload for debugging

tabular_export.core.export_to_excel_response(filename, *args, **kwargs)[source]

Returns a downloadable HttpResponse using an XLSX payload generated from headers and rows

tabular_export.core.flatten_queryset(qs, field_names=None, extra_verbose_names=None)[source]

Return a tuple of named column headers and a list of data values

By default headers will use the keys from qs.values() and rows will use the more-efficient values_list().

If a list of field_names are passed, only the included fields will be returned.

An optional dictionary of extra_verbose_names may be passed to provide friendly names for fields and will override the field’s verbose_name attribute if present. This can be used to provide proper names for related lookups (e.g. {“institution__title”: “Institution”}) or calculated values (e.g. {“items__count”: “Item Count”}).

tabular_export.core.get_field_names_from_queryset(qs)[source]

Return a list of field names for a queryset, including extra and aggregate columns

tabular_export.core.return_debug_reponse(f)[source]

Returns a debugging-friendly HTML response when TABULAR_RESPONSE_DEBUG is set

tabular_export.core.set_content_disposition(f)[source]

Ensure that an HttpResponse has the Content-Disposition header set using the input filename= kwarg