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.
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 respone 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)
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)
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
Hashes for django-queryset-csv-0.2.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 486521b66bd90a43d1d137021a9e6023aabd5f97c753e3aaccab148fb343efd8 |
|
MD5 | 8f20274fbfc79a424db4fcd4258c9681 |
|
BLAKE2b-256 | e56dfdbb203d90356a537e63e6d1d26fa76aee017695bfba8dc7ad63abb8d6a4 |