Using Django REST Framework for CSV export

Just yesterday I came (again) across a need to implement CSV file export on yet another site. I googled a bit to check existing libraries, but most of them was really complex (mostly made to handle admin level import and exports) or weren't updated for years.

App that I worked on rocked Django 1.8 on python3.4 and I just needed to serialize a model and generate CSV (basically from ORM QuerySet).

Why not use Django REST Framework (that I already use almost everywhere) to do the heavy lifting of serialization based on QuerySet?

Just assume that we use this simple model:

We need to build basic `ModelSerializer` like this:

and now, create simple Django view, that will create serializer based on out QuerySet, then use DictWriter from standard library csv module to create CSV. No manual type conversion, no list handling, no DateTime formatting. It just works.

Warning:

This code samples are not tested, treat it as pseudo-code

Used python3.4 with Django==1.8.2 and djangorestframework==3.1.3.