django-queryset-csv


Namedjango-queryset-csv JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttp://github.com/azavea/django-queryset-csv
SummaryA simple python module for writing querysets to csv
upload_time2016-11-02 16:41:24
maintainer
docs_urlNone
authorMichael Maurizi
requires_python
license
keywords django queryset csv
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            .. image:: https://travis-ci.org/azavea/django-queryset-csv.png
   :target: https://travis-ci.org/azavea/django-queryset-csv/

.. image:: https://coveralls.io/repos/azavea/django-queryset-csv/badge.svg?branch=master&service=github
   :target: https://coveralls.io/r/azavea/django-queryset-csv/

.. image:: https://img.shields.io/pypi/dm/Django.svg
   :target: http://pypi.python.org/pypi/django-queryset-csv/

.. image:: https://img.shields.io/pypi/v/django-queryset-csv.svg
   :target: http://pypi.python.org/pypi/django-queryset-csv/

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. <http://www.azavea.com/blogs/labs/2014/03/exporting-django-querysets-to-csv/>`_

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 <https://docs.python.org/2/library/csv.html#csv.DictWriter>`_.


development and contributions
-----------------------------

Please read the included ``CONTRIBUTING.rst`` file.
            

Raw data

            {
    "_id": null,
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "cheesecake_code_kwalitee_id": null,
    "keywords": "django queryset csv",
    "upload_time": "2016-11-02 16:41:24",
    "author": "Michael Maurizi",
    "home_page": "http://github.com/azavea/django-queryset-csv",
    "github_user": "azavea",
    "download_url": "https://pypi.python.org/packages/06/ee/77085a6ca641dc5ada7dcc492d981dad27e9f8e9228f568bbf0f7e55662c/django-queryset-csv-1.0.0.tar.gz",
    "platform": "UNKNOWN",
    "version": "1.0.0",
    "cheesecake_documentation_id": null,
    "description": ".. image:: https://travis-ci.org/azavea/django-queryset-csv.png\n   :target: https://travis-ci.org/azavea/django-queryset-csv/\n\n.. image:: https://coveralls.io/repos/azavea/django-queryset-csv/badge.svg?branch=master&service=github\n   :target: https://coveralls.io/r/azavea/django-queryset-csv/\n\n.. image:: https://img.shields.io/pypi/dm/Django.svg\n   :target: http://pypi.python.org/pypi/django-queryset-csv/\n\n.. image:: https://img.shields.io/pypi/v/django-queryset-csv.svg\n   :target: http://pypi.python.org/pypi/django-queryset-csv/\n\na CSV exporter for django querysets.\n\nThis tool was created out of repeatedly needing to do the following in django:\n\n1. write CSV data that is based on simple querysets.\n2. automatically encode unicode characters to utf-8\n3. create a shortcut to render a queryset to a CSV HTTP response\n4. add a time/datestamping mechanism to CSV filenames\n\nFor more detailed documentation, please read `this blog post. <http://www.azavea.com/blogs/labs/2014/03/exporting-django-querysets-to-csv/>`_\n\ninstallation\n------------\n\nRun::\n\n   pip install django-queryset-csv\n\nSupports Python 2.7 and 3.5, Django >= 1.8.\n\nusage\n-----\nPerform all filtering and field authorization in your view using ``.filter()`` and ``.values()``.\nThen, use ``render_to_csv_response`` to turn a queryset into a response with a CSV attachment.\nPass it a ``QuerySet`` or ``ValuesQuerySet`` instance::\n\n  from djqscsv import render_to_csv_response\n\n  def csv_view(request):\n    qs = Foo.objects.filter(bar=True).values('id', 'bar')\n    return render_to_csv_response(qs)\n\nIf you need to write the CSV to a file you can use ``write_csv`` instead::\n\n  from djqscsv import write_csv\n\n  qs = Foo.objects.filter(bar=True).values('id', 'bar')\n  with open('foo.csv', 'w') as csv_file:\n    write_csv(qs, csv_file)\n\nforeign keys\n------------\n\nForeign keys are supported natively using ``ValuesQuerySet`` in Django. Simply use the ``__`` technique as \nyou would in the Django ORM when you pass args to the ``.values()`` method.\n\nmodels.py::\n\n  from django.db import models\n\n  class Food(models.Model):\n      name = models.CharField(max_length=20)\n\n  class Person(models.Model):\n      name = models.CharField(max_length=20)\n      favorite_food = models.ForeignKey(Food)\n\nviews.py::\n\n  from djqscsv import render_to_csv_response\n\n  def csv_view(request):\n      people = Person.objects.values('name', 'favorite_food__name')\n      return render_to_csv_response(people)\n\nkeyword arguments\n-----------------\n\nThis 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:\n\n- ``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.\n- ``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``.\n- ``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.\n- ``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.\n\nIn addition to the above arguments, ``render_to_csv_response`` takes the following optional keyword argument:\n\n- ``streaming`` - (default: ``True``) A boolean determining whether to use ``StreamingHttpResponse`` instead of the normal ``HttpResponse``.\n\nThe remaining keyword arguments are *passed through* to the csv writer. For example, you can export a CSV with a different delimiter.\n\nviews.py::\n\n  from djqscsv import render_to_csv_response\n\n  def csv_view(request):\n      people = Person.objects.values('name', 'favorite_food__name')\n      return render_to_csv_response(people, delimiter='|')\n\nFor more details on possible arguments, see the documentation on `DictWriter <https://docs.python.org/2/library/csv.html#csv.DictWriter>`_.\n\n\ndevelopment and contributions\n-----------------------------\n\nPlease read the included ``CONTRIBUTING.rst`` file.",
    "lcname": "django-queryset-csv",
    "name": "django-queryset-csv",
    "github": true,
    "bugtrack_url": "",
    "license": "",
    "travis_ci": true,
    "github_project": "django-queryset-csv",
    "summary": "A simple python module for writing querysets to csv",
    "split_keywords": [
        "django",
        "queryset",
        "csv"
    ],
    "author_email": "mmaurizi@azavea.com",
    "urls": [
        {
            "has_sig": false,
            "upload_time": "2016-11-02T16:41:24",
            "comment_text": "",
            "python_version": "source",
            "url": "https://pypi.python.org/packages/06/ee/77085a6ca641dc5ada7dcc492d981dad27e9f8e9228f568bbf0f7e55662c/django-queryset-csv-1.0.0.tar.gz",
            "md5_digest": "c91d479116beec7e91bb60a5cf5afd18",
            "downloads": 0,
            "filename": "django-queryset-csv-1.0.0.tar.gz",
            "packagetype": "sdist",
            "path": "06/ee/77085a6ca641dc5ada7dcc492d981dad27e9f8e9228f568bbf0f7e55662c/django-queryset-csv-1.0.0.tar.gz",
            "size": 20211
        },
        {
            "has_sig": false,
            "upload_time": "2016-11-02T16:41:26",
            "comment_text": "",
            "python_version": "2.7",
            "url": "https://pypi.python.org/packages/5e/5b/82bdf52d68cbb9f07ad34e4177b36a4c5baf9a3812efc9e82308f291f66f/django_queryset_csv-1.0.0-py2.7.egg",
            "md5_digest": "3e9b386f047c52a59091bd7c1802527e",
            "downloads": 0,
            "filename": "django_queryset_csv-1.0.0-py2.7.egg",
            "packagetype": "bdist_egg",
            "path": "5e/5b/82bdf52d68cbb9f07ad34e4177b36a4c5baf9a3812efc9e82308f291f66f/django_queryset_csv-1.0.0-py2.7.egg",
            "size": 10973
        }
    ],
    "cheesecake_installability_id": null,
    "coveralls": true
}
        
Elapsed time: 0.01738s