A Django library that adds a ListView Mixin for downloading a list in different file formats
Project description
# django-export-download
`django-export-download` allows you to use your ListView to download it in different file formats
like CSV or XLS by just adding the Mixin and add a new URL to your `urls.py`
It provides a MultipleObject/ListView Mixin for Django and add a classmethod `as_download()` method.
You can use this view to download the object list in different file fomats like CSV, XLS and more.
You just have to provide a `Resource` class from `django-import-export`.
I addition it work very good in you `django-tables2`/`django-filter` environment.
Finally the package ships with a templatetag to include some download buttons in your ListView template.
# Requirements
I tested the package with following versions, but it also should
work with newer or older versions
* Django >= 1.11
* django-import-export >= 1.0.0
* Python >= 3.5
# Quickstart
```python
views.py:
from import_export import resources
from export_download.views import ExportDownloadView
from django.views.generic import ListView
class MovieBudgetResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget']
class MovieListView(ExportDownloadView, ListView):
model = Movie
resource_class = MovieBudget
urls.py:
urlpatterns = [
path('movie/', MovieListView.as_view(), name='movie-list'),
path('movie/export/', MovieListView.as_download(), name='movie-export'),
]
```
By visiting http://localhost:8000/movie/export you can download a CSV (which is the default) file with the movies
and their budget.
http://localhost:8000/movie/export?resource_format=xls will download a Excel file.
# Dokumentation
## Class Options
Here is a more complex example that includes several `Resources` and different file formats.
It also is a example how to use `django-export-download` with `django-filter` and `django-tables2`.
```python
import django_tables2 as table
from import_export import resources
from export_download.views import ExportDownloadView
from django.views.generic import ListView
class MovieResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget', 'tatus', 'votes', 'release_date']
class OnlyMovieResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title']
class MovieBudgetResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget']
class MovieListView(ExportDownloadView, ListView, table.SingleTableMixin):
model = Movie
table_class = MovieTable
filter_class = MovieFilter
resource_class = [
MovieResource,
OnlyMovieResource,
MovieBudgetResource
]
resource_formats = ['csv', 'tsv', 'xls']
```
This implementation support 3 download formats with 3 different `Resources`. Following URLs are giving
you the files:
```
http://localhost:8000/movie/export/?resource_class=0&resource_format=xls
http://localhost:8000/movie/export/?resource_class=0&resource_format=csv
http://localhost:8000/movie/export/?resource_class=0&resource_format=tsv
http://localhost:8000/movie/export/?resource_class=1&resource_format=xls
http://localhost:8000/movie/export/?resource_class=1&resource_format=csv
http://localhost:8000/movie/export/?resource_class=1&resource_format=tsv
http://localhost:8000/movie/export/?resource_class=2&resource_format=xls
http://localhost:8000/movie/export/?resource_class=2&resource_format=csv
http://localhost:8000/movie/export/?resource_class=2&resource_format=tsv
```
Note that `resource_class` defines the position of the `Resource` implementation in the list of `resource_class`
In this example there also is a `filter_class`. `django-export-download` automatically applies the filter
to the queryset. It is not required, but works really well. Have a look at https://github.com/carltongibson/django-filter
for more information.
## Templatetags
`django-export-download` ships with a templatetag to render all links above. To use it your
download URL should point to the view name `<model_name>-export` or you have to overwrite the (simple) method
```python
def get_export_url(self, resource_class):
return reverse('{}-export'.format(resource_class.Meta.model._meta.model_name))
```
You now can use the templatetag in you ListView
```html
{% load export_download %}
{% export_download_menu %}
```
# Contribute
Fork and send a PR
`django-export-download` allows you to use your ListView to download it in different file formats
like CSV or XLS by just adding the Mixin and add a new URL to your `urls.py`
It provides a MultipleObject/ListView Mixin for Django and add a classmethod `as_download()` method.
You can use this view to download the object list in different file fomats like CSV, XLS and more.
You just have to provide a `Resource` class from `django-import-export`.
I addition it work very good in you `django-tables2`/`django-filter` environment.
Finally the package ships with a templatetag to include some download buttons in your ListView template.
# Requirements
I tested the package with following versions, but it also should
work with newer or older versions
* Django >= 1.11
* django-import-export >= 1.0.0
* Python >= 3.5
# Quickstart
```python
views.py:
from import_export import resources
from export_download.views import ExportDownloadView
from django.views.generic import ListView
class MovieBudgetResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget']
class MovieListView(ExportDownloadView, ListView):
model = Movie
resource_class = MovieBudget
urls.py:
urlpatterns = [
path('movie/', MovieListView.as_view(), name='movie-list'),
path('movie/export/', MovieListView.as_download(), name='movie-export'),
]
```
By visiting http://localhost:8000/movie/export you can download a CSV (which is the default) file with the movies
and their budget.
http://localhost:8000/movie/export?resource_format=xls will download a Excel file.
# Dokumentation
## Class Options
Here is a more complex example that includes several `Resources` and different file formats.
It also is a example how to use `django-export-download` with `django-filter` and `django-tables2`.
```python
import django_tables2 as table
from import_export import resources
from export_download.views import ExportDownloadView
from django.views.generic import ListView
class MovieResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget', 'tatus', 'votes', 'release_date']
class OnlyMovieResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title']
class MovieBudgetResource(resources.ModelResource):
class Meta:
model = Movie
fields = ['title', 'budget']
class MovieListView(ExportDownloadView, ListView, table.SingleTableMixin):
model = Movie
table_class = MovieTable
filter_class = MovieFilter
resource_class = [
MovieResource,
OnlyMovieResource,
MovieBudgetResource
]
resource_formats = ['csv', 'tsv', 'xls']
```
This implementation support 3 download formats with 3 different `Resources`. Following URLs are giving
you the files:
```
http://localhost:8000/movie/export/?resource_class=0&resource_format=xls
http://localhost:8000/movie/export/?resource_class=0&resource_format=csv
http://localhost:8000/movie/export/?resource_class=0&resource_format=tsv
http://localhost:8000/movie/export/?resource_class=1&resource_format=xls
http://localhost:8000/movie/export/?resource_class=1&resource_format=csv
http://localhost:8000/movie/export/?resource_class=1&resource_format=tsv
http://localhost:8000/movie/export/?resource_class=2&resource_format=xls
http://localhost:8000/movie/export/?resource_class=2&resource_format=csv
http://localhost:8000/movie/export/?resource_class=2&resource_format=tsv
```
Note that `resource_class` defines the position of the `Resource` implementation in the list of `resource_class`
In this example there also is a `filter_class`. `django-export-download` automatically applies the filter
to the queryset. It is not required, but works really well. Have a look at https://github.com/carltongibson/django-filter
for more information.
## Templatetags
`django-export-download` ships with a templatetag to render all links above. To use it your
download URL should point to the view name `<model_name>-export` or you have to overwrite the (simple) method
```python
def get_export_url(self, resource_class):
return reverse('{}-export'.format(resource_class.Meta.model._meta.model_name))
```
You now can use the templatetag in you ListView
```html
{% load export_download %}
{% export_download_menu %}
```
# Contribute
Fork and send a PR
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
Built Distribution
Close
Hashes for django-export-download-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcab01dd929ec0025d536dae2b723a009d294c9b0d37acb671f37dbf6fefc470 |
|
MD5 | 1e0cd4429b4c57987da6d565ea6e3187 |
|
BLAKE2b-256 | 72b0ed6ffad36cfaa8aa9d20d4a6e1d88522ccf9a29b80e1bb3e2d34d8310352 |
Close
Hashes for django_export_download-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 039d85ca602c4ce3b52b6ce2623c288d09bd300bc6d5c1cde914fa0dd16c9f30 |
|
MD5 | e2a4748218702be90daf1a62131a9ede |
|
BLAKE2b-256 | 57ab3a3fbf9a507a6176d50110fcb3427c9a924b9c56a663ccff893ff18cfbec |