Skip to main content

Simple model to csv conversions for Django.

Project description

Build Status

Django-CSVExport

This is a django extension that simplifies model to csv conversions.

Requirements

  • Python 2.7

  • Django 1.6+

Example:

Simple usage:

Generate csv string from a model:

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:

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

url(r'^csv/$', 'app.views.gen_csv'),

views.py

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:

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

Genres 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.

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-csvexport-1.0.5.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file django-csvexport-1.0.5.tar.gz.

File metadata

File hashes

Hashes for django-csvexport-1.0.5.tar.gz
Algorithm Hash digest
SHA256 9e62ba602982dfc6b2e0b0f89d1cb0bd5f92197e7c7dd94db542dec2ac641087
MD5 8eaba46ba3198955449956d87033e79a
BLAKE2b-256 33a2cd476ffb09d601a8d59f44d501fe4c51c5bfc9cc0fc43b5ad35cd0be2ec5

See more details on using hashes here.

Supported by

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