Giter Site home page Giter Site logo

django-queryset-csv's Introduction

image

image

image

image

a CSV exporter for django querysets.

This tool was created out of repeatedly needing to do the following in django:

  1. write CSV data that is based on simple querysets.
  2. automatically encode unicode characters to utf-8
  3. create a shortcut to render a queryset to a CSV HTTP response
  4. 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.6 and 2.7, Django >= 1.5.

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)

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.

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.

django-queryset-csv's People

Contributors

luanfonceca avatar meteozond avatar rickmohr avatar smileychris avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.