django-export-csv
is a reusable Django application for which provides generic views for downloading CSV files.
To get started using django-export-csv
simply install it with
pip install django-csv-export
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.
Note
All examples follow from the models in the example project.
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)
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']
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'