Skip to main content

Django REST Spreadsheets renderer

This project pulls work from drf-renderer-xlsx and django-rest-framework-csv aiming to combine these renderers into a single package while fixing some of their individual quirks.

Differences

An important use case for file export in a RESTful API is to allow the user to manipulate data using their own tools. While pagination is useful for endpoints where you don't need to see all the data, they don't make sense for these types of file export. This project will handle pagination by ignoring it, serializing the entire queryset.


In addition, to enable CSV/XLSX on an endpoint, all that needs to be done is adding a single mixin like such:

from drf_spreadsheets.mixins import SpreadsheetMixIn

class YourViewSet(SpreadsheetMixIn, ModelViewSet):
    ...

This instead of manually overriding renderer_classes, or adding the renderer to the defaults. The former is clunky and can lead to unwanted behavior, and the latter is rarely necessary since there will be many endpoints that don't need spreadsheet exports in most projects.


One regression is in XLSX styling. For the initial version of this package, no styling can be applied to generated XLSX files. Since this is not an essential feature for most export use-cases, it has been left as a potential future feature

Usage

To use this package, simply add the SpreadsheetMixIn to a ViewSet! By default, both CSV and XLSX formats will be available on that view. There are six fields provided by SpreadsheetMixIn:

field default use
enable_csv True Allows CSV format to be accessed on that particular ViewSet
enable_xlsx True Allows XLSX format to be accessed on that particular ViewSet
enable_spreadsheets_on_details False Allows the CSV and/or XLSX renderers to be used on detail views (ie not list views)
enable_renderer_defaults True Allows the default renderers found in api_settings to also be used. If set to false, exclusively the spreadsheet renderers will be used
filename None Overrides the auto-generated filename of the form "<Model name> Report" or "<View name> Report" if no model is available
spreadsheet_headers None Overrides the auto-generated columns with either a list or a dictionary. If it is set to be a dictionary, the keys will be columns to include and the values will be their names/labels.
compact_columns False Renders secondary data structures as json strings instead of creating new columns for their children

An alternative to providing spreadsheet_headers as a field through SpreadsheetMixIn is providing it via overriding the get_renderer_context() method. For example

    def get_renderer_context(self):
        context = super(MyViewSet, self).get_renderer_context()
        if 'headers' in self.request.GET:
            context["spreadsheet_headers"] = self.request.GET['headers'].split(',')
        return context

Release files for drf-spreadsheets 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-spreadsheets 1.1.3
File Size Uploaded
drf-spreadsheets-1.1.3.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-spreadsheets 1.1.3
File Interpreter ABI Platform
drf_spreadsheets-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 13.2 kB

Release files / drf-spreadsheets-1.1.3.tar.gz

Download URL drf-spreadsheets-1.1.3.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6aeba7f645acb5c8de79956f9a5fa47a8627e48ccfecb3a1528fd51b84dc8eb9
BLAKE2b-256 checksum
How to use checksums
25adc4a5060e9c5f023927e69cffc387343033060fc5296aff28b24c358765f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

Release files / drf_spreadsheets-1.1.3-py3-none-any.whl

Download URL drf_spreadsheets-1.1.3-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51fa0f0afe325b33232f722b9eb4e9b15e8adf0cff5ab1c96d459161834bdaeb
BLAKE2b-256 checksum
How to use checksums
f9213dd1e024cfb78422c463962b64f4269ccefb040e69ffa51536049145b9d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page