A Django REST Framework package allowing views to be rendered as CSV or XLSX
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drf-spreadsheets-1.1.3.tar.gz
.
File metadata
- Download URL: drf-spreadsheets-1.1.3.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6aeba7f645acb5c8de79956f9a5fa47a8627e48ccfecb3a1528fd51b84dc8eb9 |
|
MD5 | 4dc52bc8aa004d1e2d34af96c4424a42 |
|
BLAKE2b-256 | 25adc4a5060e9c5f023927e69cffc387343033060fc5296aff28b24c358765f5 |
File details
Details for the file drf_spreadsheets-1.1.3-py3-none-any.whl
.
File metadata
- Download URL: drf_spreadsheets-1.1.3-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51fa0f0afe325b33232f722b9eb4e9b15e8adf0cff5ab1c96d459161834bdaeb |
|
MD5 | 0e0be7f766154b91b928a4cfbc5c9090 |
|
BLAKE2b-256 | f9213dd1e024cfb78422c463962b64f4269ccefb040e69ffa51536049145b9d9 |