Package: djangorestframework-csv-3
djangorestframework-csv-3Up-to-date CSV Tools for Django REST FrameworkThis project is a forkdjango-rest-framework-csvbyMjumbe Wawatu Poeand contributors. Original authors do not endorse this fork.The goal of this fork is to keep the project up-to-date with the latest versions of Django and Python, but also:Use modern Python packaging tools (PEP 517/518, PDM support)Provide type hints and use type checking tools (mypy)Remove deprecated featuresInstallationpipinstalldjangorestframework-csv-3or withPDM:pdmadddjangorestframework-csv-3or withPoetry:poetryadddjangorestframework-csv-3Usageviews.pyfromrest_framework.viewsimportAPIViewfromrest_framework.settingsimportapi_settingsfromrest_framework_csvimportrenderersasrclassMyView(APIView):renderer_classes=(r.CSVRenderer,)+tuple(api_settings.DEFAULT_RENDERER_CLASSES)...Alternatively, to set CSV as a default rendered format, add the
following to thesettings.pyfile:REST_FRAMEWORK={# specifying the renderers'DEFAULT_RENDERER_CLASSES':('rest_framework_csv.renderers.CSVRenderer',),}Ordered FieldsBy default, aCSVRendererwill output fields in sorted order. To
specify an alternative field ordering, you can override theheaderattribute. There are two ways to do this:Create a new renderer class and override theheaderattribute
directly:classMyUserRenderer(CSVRenderer):header=['first','last','email']@api_view(['GET'])@renderer_classes((MyUserRenderer,))defmy_view(request):users=User.objects.filter(active=True)content=[{'first':user.first_name,'last':user.last_name,'email':user.email}foruserinusers]returnResponse(content)Use therenderer_contextto override the field ordering on the
fly:classMyView(APIView):renderer_classes=[CSVRenderer]defget_renderer_context(self):context=super().get_renderer_context()context['header']=(self.request.GET['fields'].split(',')if'fields'inself.request.GETelseNone)returncontext...Labeled FieldsCustom labels can be applied to theCSVRendererusing thelabelsdict attribute where each key corresponds to the header and the value
corresponds to the custom label for that header.Create a new renderer class and override theheaderandlabelsattribute directly:classMyBazRenderer(CSVRenderer):header=['foo.bar']labels={'foo.bar':'baz'}PaginationUsing the renderer with paginated data is also possible with the newPaginatedCSVRendererclass and should be used with views
that paginate dataFor more information about using renderers with Django REST Framework,
see theAPI Guideor theTutorial.What about other tabular formats?If you're looking for other formats, check out the following packages:drf-excelby @wharton which supports Excel format with advanced customizationdjango-rest-pandasby @wq, which supports XLS, XLSX, CSV, TXT, SVG and moreRunning the testsTo run the tests against the current environment:./manage.pytestChangelogThis project was forked starting from release 3.0.0.3.0.0Drop support for unsupported versions of Django and PythonDjango: 3.2, 4.1 and 4.2 are supportedPython: 3.8, 3.9, 3.10 and 3.11 are supportedRemoved deprecatedwriter_optsparameter fromCSVRenderer.render(...). Pass it in thecontext_rendereror on the class.Removed deprecatedheadersproperty fromCSVRendererRendering is now exclusivelyUTF-8encodedCSVRenderernow usesio.StringIOinstead ofio.BytesIOAddedget_headers(...)toCSVRenderer, used intablize(...)to ease customization2.1.1Add support for byte order markers (BOM) (thanks @Yaoxin)Documentation updates (thanks @rjemanuele and @coreyshirk)2.1.0CSVs with no data still output header labels (thanks @travisbloom)Include a paginated renderer as part of the app (thanks
@masterfloda)Generators can be used as data sources for CSVStreamingRenderer
(thanks @jrzerr)Support for non UTF-8 encoding parsing (thanks @weasellin)2.0.0MakeCSVRenderer.renderreturn bytes, andCSVParser.parseexpect a byte stream.Have data-less renders print header row, if header is explicitly
suppliedDrop Django 1.7 tests and add Django 1.10 testshaveCSVRenderer.tablizeact as a generator when
possible (i.e., when a header is explicitly specified).Add docs for labels thanks to @radyzFix header rendering inCSVStreamingRendererthanks to
@radialnashImprove unicode handling, thanks to @brandonrobertz1.4.0/1.4.1Add support for changing field labels in theCSVRenderer, thanks
to @sobyAdd support for settingCSVRendererheaders, labels, and
writer_opts asrenderer_contextparameters.RenamedCSVRenderer.headerstoCSVRenderer.header; old spelling
is still available for backwards compatibility, but may be removed
in the future.1.3.4Support streaming CSV rendering, via @ivancrnetoImproved test configuration and project metadata, via @ticosax1.3.2/1.3.3Support unicode CSV parsing, and universal newlines, with thanks to
@brocksamson1.3.1Renderer handles case where data is not a list by wrapping data in a
list, via pull request from @dougvkBetter cross Python version support, via @paurullan and @vishen1.3.0Support for Python 3, derived from work by @samdobson1.2.0Support consistent ordering of fields in rendered CSV; thanks to
@robguttmanSupport specifying particular fields/headers in custom CSV renderer
by overriding theheadersattribute.1.1.0Support simple CSV parsing; thanks to @sebastibe1.0.1Add the package manifest1.0.0Initial release