Skip to main content

Django class-based view for CSV exports

Project description

django-csv-export-view

A Django class-based view for CSV export.

Build Status

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

Quick Start

Examples:

class DataExportView(CSVExportView):
    model = Data
    fields = ('field1', 'field2__related_field', 'property1')

class DataExportView(CSVExportView):
    model = Data
    fields = '__all__'

class DataExportView(CSVExportView):
    model = Data
    exclude = ('id',)

    def get_queryset(self):
        queryset = super(DataExportView, self).get_queryset()
        return queryset.exclude(deleted=True)

class DataExportView(CSVExportView):
    model = Data

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

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.

Further Customization

Examples:

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

class DataExportView(CSVExportView):
    model = Data
    fields = '__all__'

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

header - boolean - Default: True
Whether or not 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 or not to include sep=<sepaator> as the first line of the CSV file. This is useful for generating Microsoft Excel friendly CSV.

CSV Writer Options

Example:

class DataExportView(CSVExportView):
    model = Data
    fields = '__all__'

    def get_csv_writer_fmtparams(self):
        fmtparams = super(DataExportView, self).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.6/library/csv.html#csv.writer

Django Admin Integration

Example:

@admin.register(Data)
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

Project details


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-1.0.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

django_csv_export_view-1.0.1-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django-csv-export-view-1.0.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for django-csv-export-view-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a6d4da40efa0f0d44178afd20dff54ff2d44b5625c362b506ae1f9f52e568c81
MD5 ef135603f7cd73c329b11042ce066a2f
BLAKE2b-256 a84b5f3ac9675d7a595285162c04f60c95515e626e863bda77c11162cbd1b147

See more details on using hashes here.

File details

Details for the file django_csv_export_view-1.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_csv_export_view-1.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for django_csv_export_view-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 178fb2be535f0b1d146ddd8cebb2f5ec6f738b7807a5600a9a234134ab7f9e92
MD5 89b83e4e2d765a4a66cd41fffe89c641
BLAKE2b-256 01586d15569b0af6ee77f2612c5ff137669906cf41ede2fec780bbeadea808c6

See more details on using hashes here.

Supported by

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