You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/renderers.md
+40Lines changed: 40 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -457,6 +457,43 @@ Modify your REST framework settings.
457
457
458
458
[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.
459
459
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
+
460
497
## CSV
461
498
462
499
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
0 commit comments