Skip to main content

Utilities for working with CSVs in Django

Project description

django-csv-utils - Tools for working with CSV data in Django
============================================================

Contains helpers for CSV interaction.

There are two primary uses:

* Running an import command with a list of importers
* Exporting a streaming CSV response export

Example import:
---------------

.. code:: python
from django_csv_utils import CSVImportException, ImportCommand, Importer
from users.models import User


class UserImporter(Importer):
"""Imports User objects from a CSV."""
header = [
"first_name",
"last_name",
"email",
]

def import_row(self, row):
errors = []
first_name = row.get("first_name", "")
last_name = row.get("last_name", "")
email = self.get_str_errblank(row, "email", errors).lower()
if User.objects.filter(email=email).exists():
errors.append("The email {} is already in use".format(email))
if errors:
raise CSVImportException(', '.join([str(e) for e in errors]))
new_user = User.objects.create(
email=email, first_name=first_name, last_name=last_name)
return "Imported {0}".format(new_user.get_full_name())


class Command(ImportCommand):
imports = {'users': UserImporter}


Example StreamingHTTPResponse:
------------------------------

.. code:: python
class UserCSVView(StreamingCSVView):
"""Give the list of users."""
header = [
'fist_name',
'last_name',
'email',
]

def get_queryset(self):
"""Return the right list of users."""
return User.objects.filter(is_active=True, is_superuser=False)

def get_row(self, item):
return (
item.first_name,
item.last_name,
item.email,
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-csv-utils-1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file django-csv-utils-1.0.1.tar.gz.

File metadata

File hashes

Hashes for django-csv-utils-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f2c5ebbfc26ddca1b818886f625a354470b7b23dc73fa26e11e10d68882a120d
MD5 4e1f942713e33ea6e9605244c269143d
BLAKE2b-256 e64b1574de0c9a687ab8b9f41a1017fa6e75684c0d2c35e9a5ce7b4175ca794c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page