# django-csv-export-view
A Django class-based view for CSV export.
[![Tests](https://github.com/benkonrath/django-csv-export-view/actions/workflows/tests.yml/badge.svg)](https://github.com/benkonrath/django-csv-export-view/actions/workflows/tests.yml)
## 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()`.
```python
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.
```python
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__`.
```python
from csv_export.views import CSVExportView
from .models import MyModel
class DataExportView(CSVExportView):
model = MyModel
fields = "__all__"
```
`exclude` can be used instead of `fields`.
```python
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.
```python
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.
```python
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.
```python
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.
```python
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:
```python
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:
```python
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
Raw data
{
"_id": null,
"home_page": "https://github.com/benkonrath/django-csv-export-view",
"name": "django-csv-export-view",
"maintainer": null,
"docs_url": null,
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,<4",
"maintainer_email": null,
"keywords": null,
"author": "Ben Konrath",
"author_email": "ben@bagu.org",
"download_url": "https://files.pythonhosted.org/packages/36/88/1b41f77992b6017fbac1faa0878b4874624765f70388bb18faadf6807ac1/django-csv-export-view-2.0.1.tar.gz",
"platform": null,
"description": "# django-csv-export-view\n\nA Django class-based view for CSV export.\n\n[![Tests](https://github.com/benkonrath/django-csv-export-view/actions/workflows/tests.yml/badge.svg)](https://github.com/benkonrath/django-csv-export-view/actions/workflows/tests.yml)\n\n## Features\n\n* Easy CSV exports by setting a Django `model` and a `fields` or `exclude` iterable\n* Works with existing class-based view mixins for access control\n* Generates Microsoft Excel friendly CSV by default\n* Proper HTTP headers set for CSV\n* Easy to override defaults as needed\n* Easy integration into Django Admin\n\n## Installation\n\n`pip install django-csv-export-view`\n\n## Examples of basic options\n\nSpecify a `model` and `fields`. Optionally override `get_queryset()`.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = (\"field\", \"related\", \"property\")\n\n # When using related fields you will likely want to override get_queryset()\n # to use select_related(), prefetch_related() or generally filter the results.\n def get_queryset(self):\n return super().get_queryset().select_related(\"related\")\n # -- OR --\n return super().get_queryset().prefetch_related(\"related\")\n # -- OR --\n return queryset.exclude(deleted=True)\n # etc\n```\n\nYou can also use related fields and properties.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = (\"field\", \"related__field\", \"property\")\n```\n\n`__all__` is supported if you want all fields. Model properties are not included with `__all__`.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = \"__all__\"\n```\n\n`exclude` can be used instead of `fields`.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n exclude = (\"id\",)\n```\n\nOverride `get_fields()` for dynamic control of the fields.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n\n def get_fields(self, queryset):\n fields = [\"username\", \"email\"]\n if self.request.user.is_superuser:\n fields.append(\"birth_date\")\n return fields\n```\n\n## Basic options\n\n`fields` / `exclude`: An iterable of field names and properties. You cannot set both `fields` and `exclude`.\n`fields` can also be `\"__all__\"` to export all fields. Model properties are not included when `\"__all__\"` is used.\nRelated field can be used with `__`. Override `get_fields(self, queryset)` for custom behaviour not supported by the\ndefault logic.\n\n`model`: The model to use for the CSV export queryset. Override `get_queryset()` if you need a custom queryset.\n\n## Examples of advanced options\n\n`header`, `specify_separator` and `filename` can be use for more customization.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = \"__all__\"\n header = False\n specify_separator = False\n filename = \"data-export.csv\"\n```\n\nUsing `verbose_names` can be turned off.\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = \"__all__\"\n verbose_names = False\n```\n\nOverride `get_filename()` for dynamic control of the filename.\n```python\nfrom django.utils import timezone\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = \"__all__\"\n\n def get_filename(self, queryset):\n return \"data-export-{!s}.csv\".format(timezone.now())\n```\n\n## Advanced options\n\n`header` - *boolean* - Default: `True` \nWhether to include the header in the CSV.\n\n`filename` - *string* - Default: Dasherized version of `verbose_name_plural` from `queryset.model`. \nOverride `get_filename(self, queryset)` if a dynamic filename is required.\n\n`specify_separator` - *boolean* - Default: `True` \nWhether to include `sep=<sepaator>` as the first line of the CSV file. This is useful for generating Microsoft\nExcel friendly CSV.\n\n`verbose_names` - *boolean* - Default: `True` \nWhether to use capitalized verbose column names in the header of the CSV file. If `False`, field names are used\ninstead.\n\n## CSV Writer Options\n\nExample:\n```python\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\nclass DataExportView(CSVExportView):\n model = MyModel\n fields = \"__all__\"\n\n def get_csv_writer_fmtparams(self):\n fmtparams = super().get_csv_writer_fmtparams()\n fmtparams[\"delimiter\"] = \"|\"\n return fmtparams\n```\n\nOverride `get_csv_writer_fmtparams(self)` and return a dictionary of csv write format parameters. Default format\nparameters are: dialect=\"excel\" and quoting=csv.QUOTE_ALL. See all available options in the Python docs:\n\nhttps://docs.python.org/3.11/library/csv.html#csv.writer\n\n## Django Admin Integration\n\nExample:\n```python\nfrom django.contrib import admin\nfrom csv_export.views import CSVExportView\nfrom .models import MyModel\n\n@admin.register(MyModel)\nclass DataAdmin(admin.ModelAdmin):\n actions = (\"export_data_csv\",)\n\n def export_data_csv(self, request, queryset):\n view = CSVExportView(queryset=queryset, fields=\"__all__\")\n return view.get(request)\n\n export_data_csv.short_description = \"Export CSV for selected Data records\"\n```\n\n## Contributions\n\nPull requests are happily accepted.\n\n## Alternatives\n\nhttps://github.com/django-import-export/django-import-export/\n\nhttps://github.com/mjumbewu/django-rest-framework-csv\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "Django class-based view for CSV exports",
"version": "2.0.1",
"project_urls": {
"Homepage": "https://github.com/benkonrath/django-csv-export-view"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0f16863c0fd40ae88c8b121bd567d2e81e566720236e737c3241ac2fc134151f",
"md5": "d7f7a2ee0a983f4cbb060c0070a75981",
"sha256": "ae6aab6938fa2653d7d8b292e06efd394847a81523a792323b281d3b0dac408b"
},
"downloads": -1,
"filename": "django_csv_export_view-2.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d7f7a2ee0a983f4cbb060c0070a75981",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,<4",
"size": 10029,
"upload_time": "2024-11-04T14:59:54",
"upload_time_iso_8601": "2024-11-04T14:59:54.704677Z",
"url": "https://files.pythonhosted.org/packages/0f/16/863c0fd40ae88c8b121bd567d2e81e566720236e737c3241ac2fc134151f/django_csv_export_view-2.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "36881b41f77992b6017fbac1faa0878b4874624765f70388bb18faadf6807ac1",
"md5": "b684654a7d5b8dda678062a2bd4c50c8",
"sha256": "c6c78b0fc8fb990cff6eaa7dac19cf208f33868b429b62d5d65208f22038432b"
},
"downloads": -1,
"filename": "django-csv-export-view-2.0.1.tar.gz",
"has_sig": false,
"md5_digest": "b684654a7d5b8dda678062a2bd4c50c8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,<4",
"size": 10317,
"upload_time": "2024-11-04T14:59:55",
"upload_time_iso_8601": "2024-11-04T14:59:55.935832Z",
"url": "https://files.pythonhosted.org/packages/36/88/1b41f77992b6017fbac1faa0878b4874624765f70388bb18faadf6807ac1/django-csv-export-view-2.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-04 14:59:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "benkonrath",
"github_project": "django-csv-export-view",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "django-csv-export-view"
}