Package: djangorestframework-csv
CSV Tools for Django REST FrameworkInstallation$pipinstalldjangorestframework-csvCompatibilityFor compatibility with Python 3 and Django < 3.2, usedjangorestframework-csv<3.0.1For compatibility with Python 2.7, usedjangorestframework-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(is_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.1) 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.Running the testsTo run the tests against the current environment:$./manage.pytestChangelog3.0.2Fix the project long description metadata3.0.1Remove Django tests for all but the currently supported Django versions
(3.2, 4.1, 4.2). Also only keep tests for Python versions compatible with
supported Django versions (3.8 - 3.11).3.0.0Removed compatibility with Python 2Added compatibility with Django up to 3.2 and 4.2
(thanks to work from @TTycho, @awais786, @arpitjain799, @tirkarthi)2.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.tableizeact 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