Simple model to csv conversions for Django.
Project description
[](https://travis-ci.org/marteinn/django-csvexport)
[](http://badge.fury.io/py/django-csvexport)
# Django-CSVExport
This is a django extension that simplifies model to csv conversions.
## Requirements
- Python 2.7 / 3.3+
- Django 1.8+
## Example:
### Simple usage:
Generate csv string from a model:
```python
from csvexport.exports import ModelCSVExporter
records = Record.objects.all()
exp = ModelExporter(queryset=records)
f = exp.to_string()
```
### With specified fields
Same as the previous example, but only expose certain fields:
```python
from csvexport.exports import ModelCSVExporter
class RecordExporter(ModelCSVExporter):
class Meta:
fields = ["album", "slug"]
exclude = ["id"]
records = Record.objects.all()
exp = RecordExporter(queryset=records)
f = exp.to_string()
```
### Generating a csv from a view:
This is a example how you could generate a downloadable csv by requesting a view, using the `render_to_csv` helper.
**urls.py**
```python
url(r'^csv/$', 'app.views.gen_csv'),
```
**views.py**
```python
def gen_csv(request):
from csvexport.exporters import ModelExporter
from csvexport.utils import render_to_csv
records = Record.objects.all()
exp = ModelExporter(queryset=records)
return render_to_csv(exp)
```
### With custom hydration
It is also possible to create a custom csv object:
```python
class RecordExporter(ModelExporter):
class Meta:
fields = ["artist", "title", "release_year", "format", "quality",
"record_label", "comment", "venue", "url"]
def hydrate_entry(self, entry):
venue_name = ""
if entry.venue:
venue_name = entry.venue.name
return {
"artist": entry.album.artist.name,
"title": entry.album.name,
"release_year": entry.album.release_year,
"format": entry.format,
"quality": entry.quality,
"record_label": entry.record_label,
"comment": entry.comment,
"venue": venue_name,
}
records = Record.objects.all()
exp = RecordExporter(queryset=records)
f = exp.to_string()
```
## Installation
django-csvexport can easily be installed through pip.
$ pip install django-csvexport
## Tests
This library include tests, just run `python runtests.py`.
You can also run separate test cases: `runtests.py tests.ViewTestCase`
## Contributing
Want to contribute? Awesome. Just send a pull request.
## License
Django-CSVExport is released under the [MIT License](http://www.opensource.org/licenses/MIT).
[](http://badge.fury.io/py/django-csvexport)
# Django-CSVExport
This is a django extension that simplifies model to csv conversions.
## Requirements
- Python 2.7 / 3.3+
- Django 1.8+
## Example:
### Simple usage:
Generate csv string from a model:
```python
from csvexport.exports import ModelCSVExporter
records = Record.objects.all()
exp = ModelExporter(queryset=records)
f = exp.to_string()
```
### With specified fields
Same as the previous example, but only expose certain fields:
```python
from csvexport.exports import ModelCSVExporter
class RecordExporter(ModelCSVExporter):
class Meta:
fields = ["album", "slug"]
exclude = ["id"]
records = Record.objects.all()
exp = RecordExporter(queryset=records)
f = exp.to_string()
```
### Generating a csv from a view:
This is a example how you could generate a downloadable csv by requesting a view, using the `render_to_csv` helper.
**urls.py**
```python
url(r'^csv/$', 'app.views.gen_csv'),
```
**views.py**
```python
def gen_csv(request):
from csvexport.exporters import ModelExporter
from csvexport.utils import render_to_csv
records = Record.objects.all()
exp = ModelExporter(queryset=records)
return render_to_csv(exp)
```
### With custom hydration
It is also possible to create a custom csv object:
```python
class RecordExporter(ModelExporter):
class Meta:
fields = ["artist", "title", "release_year", "format", "quality",
"record_label", "comment", "venue", "url"]
def hydrate_entry(self, entry):
venue_name = ""
if entry.venue:
venue_name = entry.venue.name
return {
"artist": entry.album.artist.name,
"title": entry.album.name,
"release_year": entry.album.release_year,
"format": entry.format,
"quality": entry.quality,
"record_label": entry.record_label,
"comment": entry.comment,
"venue": venue_name,
}
records = Record.objects.all()
exp = RecordExporter(queryset=records)
f = exp.to_string()
```
## Installation
django-csvexport can easily be installed through pip.
$ pip install django-csvexport
## Tests
This library include tests, just run `python runtests.py`.
You can also run separate test cases: `runtests.py tests.ViewTestCase`
## Contributing
Want to contribute? Awesome. Just send a pull request.
## License
Django-CSVExport is released under the [MIT License](http://www.opensource.org/licenses/MIT).
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
File details
Details for the file django-csvexport-1.1.2.tar.gz.
File metadata
- Download URL: django-csvexport-1.1.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d6cdfe8816677a8fe0a4439c82d6e02bb69dea5b06f9a4844e28b0983cce6a8
|
|
| MD5 |
6dc9f98f5f819ec9457c533940314c1d
|
|
| BLAKE2b-256 |
809a66008e64f308c98882ceb5eb96474b2f585c8bc3e919162ee54dd5655601
|