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:
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:
from django_csv_utils import StreamingCSVView
from users.models import User
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,
)
Release files for django-csv-utils 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-csv-utils-1.0.2.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_csv_utils-1.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / django-csv-utils-1.0.2.tar.gz
| Download URL | django-csv-utils-1.0.2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db5f71be39ac863de8735848dd8e0e37a974e19f6c5ee70a824a1f33960d8645
|
|
BLAKE2b-256 checksum How to use checksums |
157faf938dc85f149faa818749c6bc13982e15dc3013cb8482eccfc2918993aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_csv_utils-1.0.2-py2.py3-none-any.whl
| Download URL | django_csv_utils-1.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b417b377e442c5ff9e28d9c658feb43c1d26743e719f096e188e95f34e99a04d
|
|
BLAKE2b-256 checksum How to use checksums |
a85614170511e7703754cacbdd7cc4dbabfae8498999ea910d6579b54536bd7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |