Skip to main content

django-csv-export-view

A Django class-based view for CSV export.

Tests

Features

  • Easy CSV exports by setting a Django model and a fields or exclude iterable
  • Works with existing class-based view mixins for access control
  • Generates Microsoft Excel friendly CSV by default
  • Proper HTTP headers set for CSV
  • Easy to override defaults as needed
  • Easy integration into Django Admin

Installation

pip install django-csv-export-view

Examples of basic options

Specify a model and fields. Optionally override get_queryset().

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = ("field", "related", "property")

    # When using related fields you will likely want to override get_queryset()
    # to use select_related(), prefetch_related() or generally filter the results.
    def get_queryset(self):
        return super().get_queryset().select_related("related")
        # -- OR --
        return super().get_queryset().prefetch_related("related")
        # -- OR --
        return queryset.exclude(deleted=True)
        # etc

You can also use related fields and properties.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = ("field", "related__field", "property")

__all__ is supported if you want all fields. Model properties are not included with __all__.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = "__all__"

exclude can be used instead of fields.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    exclude = ("id",)

Override get_fields() for dynamic control of the fields.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel

    def get_fields(self, queryset):
        fields = ["username", "email"]
        if self.request.user.is_superuser:
            fields.append("birth_date")
        return fields

Basic options

fields / exclude: An iterable of field names and properties. You cannot set both fields and exclude. fields can also be "__all__" to export all fields. Model properties are not included when "__all__" is used. Related field can be used with __. Override get_fields(self, queryset) for custom behaviour not supported by the default logic.

model: The model to use for the CSV export queryset. Override get_queryset() if you need a custom queryset.

Examples of advanced options

header, specify_separator and filename can be use for more customization.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = "__all__"
    header = False
    specify_separator = False
    filename = "data-export.csv"

Using verbose_names can be turned off.

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = "__all__"
    verbose_names = False

Override get_filename() for dynamic control of the filename.

from django.utils import timezone
from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = "__all__"

    def get_filename(self, queryset):
        return "data-export-{!s}.csv".format(timezone.now())

Advanced options

header - boolean - Default: True
Whether to include the header in the CSV.

filename - string - Default: Dasherized version of verbose_name_plural from queryset.model.
Override get_filename(self, queryset) if a dynamic filename is required.

specify_separator - boolean - Default: True
Whether to include sep=<sepaator> as the first line of the CSV file. This is useful for generating Microsoft Excel friendly CSV.

verbose_names - boolean - Default: True
Whether to use capitalized verbose column names in the header of the CSV file. If False, field names are used instead.

CSV Writer Options

Example:

from csv_export.views import CSVExportView
from .models import MyModel

class DataExportView(CSVExportView):
    model = MyModel
    fields = "__all__"

    def get_csv_writer_fmtparams(self):
        fmtparams = super().get_csv_writer_fmtparams()
        fmtparams["delimiter"] = "|"
        return fmtparams

Override get_csv_writer_fmtparams(self) and return a dictionary of csv write format parameters. Default format parameters are: dialect="excel" and quoting=csv.QUOTE_ALL. See all available options in the Python docs:

https://docs.python.org/3.11/library/csv.html#csv.writer

Django Admin Integration

Example:

from django.contrib import admin
from csv_export.views import CSVExportView
from .models import MyModel

@admin.register(MyModel)
class DataAdmin(admin.ModelAdmin):
    actions = ("export_data_csv",)

    def export_data_csv(self, request, queryset):
        view = CSVExportView(queryset=queryset, fields="__all__")
        return view.get(request)

    export_data_csv.short_description = "Export CSV for selected Data records"

Contributions

Pull requests are happily accepted.

Alternatives

https://github.com/django-import-export/django-import-export/

https://github.com/mjumbewu/django-rest-framework-csv

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-export-view-2.0.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_csv_export_view-2.0.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file django-csv-export-view-2.0.1.tar.gz.

File metadata

  • Download URL: django-csv-export-view-2.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for django-csv-export-view-2.0.1.tar.gz
Algorithm Hash digest
SHA256 c6c78b0fc8fb990cff6eaa7dac19cf208f33868b429b62d5d65208f22038432b
MD5 b684654a7d5b8dda678062a2bd4c50c8
BLAKE2b-256 36881b41f77992b6017fbac1faa0878b4874624765f70388bb18faadf6807ac1

See more details on using hashes here.

File details

Details for the file django_csv_export_view-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_csv_export_view-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae6aab6938fa2653d7d8b292e06efd394847a81523a792323b281d3b0dac408b
MD5 d7f7a2ee0a983f4cbb060c0070a75981
BLAKE2b-256 0f16863c0fd40ae88c8b121bd567d2e81e566720236e737c3241ac2fc134151f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

1.1.0

1 file

1.0.1

2 files

1.0.0

2 files

0.5.0

2 files

0.4.0

1 file

0.3.0

1 file

0.2.0

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page