Skip to content

Commit bc573d8

Browse files
FlipperPAtomchristie
authored andcommitted
Add drf-renderer-xlsx package. (#6147)
* Add drf-renderer-xlsx package. * Gah, tabs creeped in; never leave your default editor environment. :)
1 parent bcc565f commit bc573d8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/api-guide/renderers.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,43 @@ Modify your REST framework settings.
457457

458458
[MessagePack][messagepack] is a fast, efficient binary serialization format. [Juan Riaza][juanriaza] maintains the [djangorestframework-msgpack][djangorestframework-msgpack] package which provides MessagePack renderer and parser support for REST framework.
459459

460+
## XLSX (Binary Spreadsheet Endpoints)
461+
462+
XLSX is the world's most popular binary spreadsheet format. [Tim Allen][flipperpa] of [The Wharton School][wharton] maintains [drf-renderer-xlsx][drf-renderer-xlsx], which renders an endpoint as an XLSX spreadsheet using OpenPyXL, and allows the client to download it. Spreadsheets can be styled on a per-view basis.
463+
464+
#### Installation & configuration
465+
466+
Install using pip.
467+
468+
$ pip install drf-renderer-xlsx
469+
470+
Modify your REST framework settings.
471+
472+
REST_FRAMEWORK = {
473+
...
474+
475+
'DEFAULT_RENDERER_CLASSES': (
476+
'rest_framework.renderers.JSONRenderer',
477+
'rest_framework.renderers.BrowsableAPIRenderer',
478+
'drf_renderer_xlsx.renderers.XLSXRenderer',
479+
),
480+
}
481+
482+
To avoid having a file streamed without a filename (which the browser will often default to the filename "download", with no extension), we need to use a mixin to override the `Content-Disposition` header. If no filename is provided, it will default to `export.xlsx`. For example:
483+
484+
from rest_framework.viewsets import ReadOnlyModelViewSet
485+
from drf_renderer_xlsx.mixins import XLSXFileMixin
486+
from drf_renderer_xlsx.renderers import XLSXRenderer
487+
488+
from .models import MyExampleModel
489+
from .serializers import MyExampleSerializer
490+
491+
class MyExampleViewSet(XLSXFileMixin, ReadOnlyModelViewSet):
492+
queryset = MyExampleModel.objects.all()
493+
serializer_class = MyExampleSerializer
494+
renderer_classes = (XLSXRenderer,)
495+
filename = 'my_export.xlsx'
496+
460497
## CSV
461498

462499
Comma-separated values are a plain-text tabular data format, that can be easily imported into spreadsheet applications. [Mjumbe Poe][mjumbewu] maintains the [djangorestframework-csv][djangorestframework-csv] package which provides CSV renderer support for REST framework.
@@ -497,6 +534,9 @@ Comma-separated values are a plain-text tabular data format, that can be easily
497534
[messagepack]: https://msgpack.org/
498535
[juanriaza]: https://github.com/juanriaza
499536
[mjumbewu]: https://github.com/mjumbewu
537+
[flipperpa]: https://githuc.com/flipperpa
538+
[wharton]: https://github.com/wharton
539+
[drf-renderer-xlsx]: https://github.com/wharton/drf-renderer-xlsx
500540
[vbabiy]: https://github.com/vbabiy
501541
[rest-framework-yaml]: https://jpadilla.github.io/django-rest-framework-yaml/
502542
[rest-framework-xml]: https://jpadilla.github.io/django-rest-framework-xml/

0 commit comments

Comments
 (0)