Export queryset to Excel workbook
How to use =========== Save the snippet to a file utils.py, and add the following view to your Django app: from django.http import HttpResponse from .utils import queryset_to_workbook def download_workbook(request): queryset = User.objects.all() columns = ( 'first_name', 'last_name', 'email', 'is_staff', 'groups') workbook = queryset_to_workbook(queryset, columns) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="export.xls"' workbook.save(response) return response Note: you can use dotted notation (`'foreign_key.foreign_key.field'`) in the columns parameter to access fields that are accessible through the objects returned by the queryset (in that case you probably want to use `select_related` with your queryset).
- export
- queryset
- xlwt