Metadata-Version: 2.1
Name: django-csv-export-view
Version: 1.0.0
Summary: Django class-based view for CSV exports
Home-page: https://github.com/benkonrath/django-csv-export-view
Author: Ben Konrath
Author-email: ben@bagu.org
License: BSD
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Topic :: Utilities
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4
Description-Content-Type: text/markdown
Requires-Dist: django (>=1.11)
Requires-Dist: unicodecsv; python_version < "3.0"

# django-csv-export-view

A Django class-based view for CSV export.

[![Build Status](https://travis-ci.org/benkonrath/django-csv-export-view.svg?branch=master)](https://travis-ci.org/benkonrath/django-csv-export-view)

## Features

* Easy CSV exports using the familiar `model` and `fields` / `exclude` pattern
* Works with your existing class-based view mixins for access control
* Generates Micosoft Excel friendly CSV by default
* Proper HTTP headers set for CSV
* Easy to override defaults as needed
* Easy itegration into Django Admin

## Quick Start

Examples:
```python
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 interable 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:
```python
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:
```python
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:
```python
@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


