A simple python module for writing querysets to csv
Project description
a CSV exporter for django querysets.
This tool was created out of repeatedly needing to do the following in django:
write CSV data that is based on simple querysets.
automatically encode unicode characters to utf-8
create a shortcut to render a queryset to a CSV HTTP response
add a time/datestamping mechanism to CSV filenames
For more detailed documentation, please read this blog post.
installation
Run:
pip install django-queryset-csv
Supports Python 2.7 and 3.5, Django >= 1.8.
usage
Perform all filtering and field authorization in your view using .filter() and .values(). Then, use render_to_csv_response to turn a queryset into a response with a CSV attachment. Pass it a QuerySet or ValuesQuerySet instance:
from djqscsv import render_to_csv_response def csv_view(request): qs = Foo.objects.filter(bar=True).values('id', 'bar') return render_to_csv_response(qs)
If you need to write the CSV to a file you can use write_csv instead:
from djqscsv import write_csv qs = Foo.objects.filter(bar=True).values('id', 'bar') with open('foo.csv', 'w') as csv_file: write_csv(qs, csv_file)
foreign keys
Foreign keys are supported natively using ValuesQuerySet in Django. Simply use the __ technique as you would in the Django ORM when you pass args to the .values() method.
models.py:
from django.db import models class Food(models.Model): name = models.CharField(max_length=20) class Person(models.Model): name = models.CharField(max_length=20) favorite_food = models.ForeignKey(Food)
views.py:
from djqscsv import render_to_csv_response def csv_view(request): people = Person.objects.values('name', 'favorite_food__name') return render_to_csv_response(people)
keyword arguments
This module exports two functions that write CSVs, render_to_csv_response and write_csv. Both of these functions require their own positional arguments. In addition, they both take the following optional keyword arguments:
field_header_map - (default: None) A dictionary mapping names of model fields to column header names. If specified, the csv writer will use these column headers. Otherwise, it will use defer to other parameters for rendering column names.
field_serializer_map - (default: {}) A dictionary mapping names of model fields to functions that serialize them to text. For example, {'created': (lambda x: x.strftime('%Y/%m/%d')) } will serialize a datetime field called created.
use_verbose_names - (default: True) A boolean determining whether to use the django field’s verbose_name, or to use it’s regular field name as a column header. Note that if a given field is found in the field_header_map, this value will take precendence.
field_order - (default: None) A list of fields to determine the sort order. This list need not be complete: any fields not specified will follow those in the list with the order they would have otherwise used.
In addition to the above arguments, render_to_csv_response takes the following optional keyword argument:
streaming - (default: True) A boolean determining whether to use StreamingHttpResponse instead of the normal HttpResponse.
The remaining keyword arguments are passed through to the csv writer. For example, you can export a CSV with a different delimiter.
views.py:
from djqscsv import render_to_csv_response def csv_view(request): people = Person.objects.values('name', 'favorite_food__name') return render_to_csv_response(people, delimiter='|')
For more details on possible arguments, see the documentation on DictWriter.
development and contributions
Please read the included CONTRIBUTING.rst file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-queryset-csv-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ba890536fb48ba9bf2a2f75d1205ac01d58c127da19e5a8e8dc096c207d4818 |
|
MD5 | c91d479116beec7e91bb60a5cf5afd18 |
|
BLAKE2b-256 | 06ee77085a6ca641dc5ada7dcc492d981dad27e9f8e9228f568bbf0f7e55662c |
Hashes for django_queryset_csv-1.0.0-py2.7.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f700ae5e28f2a3571f8101dd66f99a2a8cd52b3d281593c7389bbb68605065f |
|
MD5 | 3e9b386f047c52a59091bd7c1802527e |
|
BLAKE2b-256 | 5e5b82bdf52d68cbb9f07ad34e4177b36a4c5baf9a3812efc9e82308f291f66f |