Skip to main content

Django CSV Downloads

Django app for tracking queryset-backed CSV downloads

Version support

The current version of the this app support Python 3.12+ and Django 5.2-6.0

What does this app do?

This app is used to track user downloads of CSVs that are derived from Django QuerySets. You provide the filename, queryset and the list of columns that you want to output.

It has a single model (CsvDownload) that tracks downloads and stores the user, filename, row count and timestamp.

Usage

The recommended way to use this app is to rely on django_csv.views.download_csv, which wraps up the creation of the download object and the generation of the CSV itself:

# DISPLAY PURPOSES ONLY: DO NOT ENABLE USER DATA DOWNLOADS IN PRODUCTION
def download_users(request: HttpRequest) -> HttpResponse:
    data = User.objects.all()
    columns = ("first_name", "last_name", "email")
    return download_csv(request.user, "users.csv", data, *columns)

Settings

There is a CSV_DOWNLOAD_MAX_ROWS setting that is used to truncate output. Defaults to 10000. This is a backstop, and can be overridden on a per use basis.

Examples

Caution: All of these examples invåolve the User model as it's ubiquitous - DO NOT DO THIS ON A PRODUCTION ENVIRONMENT.

Example of writing a QuerySet to a file:

>>> data = User.objects.all()
>>> columns = ("first_name", "last_name", "email")
>>> with open('users.csv', 'w') as csvfile:
>>>     csv.write_csv(csvfile, data, *columns)
10  #<--- row count

adding a custom header:

>>> data = User.objects.all()
>>> columns = ("first_name", "last_name", "email")
>>> column_headers = ("given_name", "family_name", "email_address")
>>> with open('users.csv', 'w') as csvfile:
>>>     csv.write_csv(csvfile, data, *columns, column_headers=column_headers)
10

Example of writing to an HttpResponse:

>>> response = HttpResponse(content_type="text/csv")
>>> response["Content-Disposition"] = 'attachment; filename="users.csv"'
>>> csv.write_csv(response, data, *columns)
10

Example of writing to an in-memory text buffer:

>>> buffer = io.StringIO()
>>> csv.write_csv(buffer, data, *columns)
10

Example of writing directly to S3:

>>> with s3.s3_upload("bucket_name", "object_key") as buffer:
...     csv.write_csv(fileobj, queryset, *columns)
10
>>> # one-line convenience function
>>> s3.write_csv_s3("bucket_name/object_key", queryset, *columns)
10

Example of writing directly to SFTP:

# requires a paramiko.SFTPClient to have been created / connected.
>>> with sft.sftp_upload(client, remote_filepath) as fileobj:
...     write_csv(fileobj, queryset, *columns)
10
>>> # one-line convenience function
>>> sftp.write_csv_sftp("sftp://user:pass@host:port/path", queryset, *columns)
10

Example of a custom admin action to download User data:

class CustomUserAdmin(UserAdmin):

    actions = ['download']
    csv_fields = ("first_name", "last_name", "email", "is_staff")
    csv_filename = "users.csv"

    def download(self, request, queryset):
        """Download selected users as a CSV."""
        return download_csv(
            user=request.user,
            filename=CustomUserAdmin.csv_filename,
            queryset=queryset,
            *CustomUserAdmin.csv_fields
        )

    download.short_description = "Download selected users"

Example CBV that restricts queryset based on request.user:

class DownloadUsers(CsvDownloadView):

    def has_permission(self, request: HttpRequest) -> bool:
        return request.user.is_authenticated

    def get_queryset(self, request: HttpRequest) -> QuerySetWriter:
        """Allow superusers to download Users."""
        if request.user.is_superuser:
            return User.objects.all().order_by("first_name", "last_name")
        return User.objects.none()

    def get_filename(self, request: HttpRequest) -> str:
        return "users.csv"

    def get_columns(self, request: HttpRequest) -> str:
        return ("first_name", "last_name")

Metadata

Release files for django-csv-downloads 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-csv-downloads 1.5.0
File Size Uploaded
django_csv_downloads-1.5.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-csv-downloads 1.5.0
File Interpreter ABI Platform
django_csv_downloads-1.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / django_csv_downloads-1.5.0.tar.gz

Download URL django_csv_downloads-1.5.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cc15d480cfd2c3092ce874ac8e07f57b441e7571ab9532ce17977c06e256d4fe
BLAKE2b-256 checksum
How to use checksums
82bda43980f685cfe07d56101fca175e44b410780be1d23ca54a84362cfa4490
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / django_csv_downloads-1.5.0-py3-none-any.whl

Download URL django_csv_downloads-1.5.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3cbb5a983b2f736942588e9aee4932a33f5772770f5de039d9a87fb6d01e711e
BLAKE2b-256 checksum
How to use checksums
d54befe02f42d3534804b4bcaac02b898d2772c8e0441d0c619dac237d876272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page