Skip to main content

django-export-csv

CI status Coverage Status Requirements Status License Status

django-export-csv is a reusable Django application for which provides generic views for downloading CSV files.

Installation

To get started using django-export-csv simply install it with

pip install django-csv-export

Usage

Add 'export_csv' to INSTALLED_APPS settings of the project.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ...
    ...
    'export_csv',
]

Subclass ExportCSV in views.py file and provide model attribute.

from export_csv.views import ExportCSV

from .models import Account

class AccountCSV(ExportCSV):
    """View for creating and rendering CSV of all Account model instances."""
    model = Customer

In urls.py, add url pointing to the view class AccountCSV declared above.

from .views import AccountCSV

urlpatterns = [
    (r'^account_csv/$', AccountCSV.as_view(), name='account-csv'),
]

That’s it. It will render a CSV file containing all the fields of all the instances of Account model.

Customizing ExportCSV View

Use custom queryset

By default, all instances of the model are included in (the queryset and) the CSV.

To provide a custom queryset, override get_queryset method to return custom queryset.

class AccountCSV(ExportCSV):
    model = Account

    def get_queryset(self):
        return Account.object.filter(is_active=True)

Only include certain fields of the model

It is possible that only some fields of the model are needed.

This can be achieved in two ways:

  • provide field_names list

  • override get_field_names method

class AccountCSV(ExportCSV):
    model = Account
    field_names = ['owner', 'account_no', 'balance']
class AccountCSV(ExportCSV):
    model = Account

    def get_field_names(self):
        return ['owner', 'account_no', 'balance']

Provide filename

By default, the CSV rendered will have filename <model>_list.csv. For example, for Account model the filename will be account_list.csv.

Custom file name can be provided using two ways.

  • provide filename attribute

  • Override get_filename method.

class AccountCSV(ExportCSV):
    model = Account
    filename = 'active_account_list.csv'

    def get_queryset(self):
        return Account.object.filter(is_active=True)
class AccountCSV(ExportCSV):
    model = Account

    def get_queryset(self):
        return Account.object.filter(is_active=True)

    def get_filename(self):
        return 'active_account_list.csv'

Metadata

Release files for django-csv-export 0.0.3

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-export 0.0.3
File Size Uploaded
django-csv-export-0.0.3.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-csv-export 0.0.3
File Interpreter ABI Platform
django_csv_export-0.0.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 14.5 kB

Release files / django-csv-export-0.0.3.tar.gz

Download URL django-csv-export-0.0.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7bcf7bc4aeac7e02a888c423fc610920bc4b7eb2ccbfdb44c6fd7fe99ab2d30c
BLAKE2b-256 checksum
How to use checksums
fe2df4e48b89687130f07ed92532c46df989ca349375a68f01470efc899cd632
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_csv_export-0.0.3-py2.py3-none-any.whl

Download URL django_csv_export-0.0.3-py2.py3-none-any.whl
Size 8.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7f61001316e25f98432cba911a8158ca8386d476cff412d5cfc8574f60cca2b8
BLAKE2b-256 checksum
How to use checksums
0e4f771a05f3cf50b98c489761487b0f7f6b96b5158a17707fabd4839dcaf287
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

1 release file

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