Skip to main content

Model data exports for Django

Project description

Build Status

Django-data-exports is a model data exports app for Django. It allows you to easily create exports for your models.

Adding this app to your project will let you create exports for your models, and customize the data that will be exported by specifying which columns to include, and which format to use.

Typical use case: display a few columns from one of your models as a HTML table to be easily copy/pasted to a spreadsheet.

Installation

pip install django-data-exports

Then add to your project’s INSTALLED_APPS. In settings.py:

INSTALLED_APPS = (
    '...',
    # whatever you already have
    '...',
    'data_exports',
)

Install the models:

./manage.py syncdb  # or ./manage.py migrate if you're using south

And finally, plug the urls to your ROOT_URLCONF:

urlpatterns = patterns(
    '',
    # ... all the other urls you already have

    # exports
    url(r'^exports/', include('data_exports.urls', namespace='data_exports')),
)

Usage

Either add exports through the admin, or use the included example views. If there’s no export format attached to an export, the data_exports/export_detail.html template will be rendered with the following context:

  • export: the export itself

  • data: a queryset of all the export.model’s instance

Using the admin

There’s nothing specific to do here: connect to the admin, and add new exports. A few things to note:

  • when you create an export, it’s not possible to add columns at first. The reason being that the model is needed to be able to populate the column names

  • when you add an export, clicking on the “save” button will have the same effect as clicking on “save and continue editing”

  • once an export is created, and is being edited, the columns can be added (and are displayed as inlines)

Using the included example views

There’s three included example views:

  • /exports/add: create a new export

  • /exports/<export slug>/columns: add columns to your export

  • /exports/<export slug>: visualize your export

There is, at the moment, no example view for the export formats.

Export columns

Column choices make use of django-inspect-model to build the list of accessible “items”. Please check this app’s documentation to know more about “items”.

Choices are built by data_exports.forms.get_choices, and will consist of all the accessible items on the exported model, and on all its related models. The only related fields accessible are those on models that are directly related, using forward or reverse OneToOne fields and forward ForeignKey fields.

Example:

class Foo(models.Model):
    name = CharField(max_length=50)
    bar = ForeignKey(Bar)

class Bar(models.Model):
    name = CharField(max_length=50)

An export of Foo will have the following column choices:

  • name: Foo.name

  • bar: Foo.bar, which is unicode(Foo.bar)

  • bar.name: Bar.name

To display the value of those columns, the included templates use data_exports.templatetags.getter_tags:

Getattribute filter

{% load getter_tags %}
{{ obj|getattribute:column }}

This is roughly equivalent to the getattr python builtin, but can cope with column choices:

  • if column doesn’t have a dot, return getattr(obj, column), or getattr(obj, column)() if it’s a callable

  • if column does have a dots (eg: bar.name), recursively call getattribute() to get to the final attribute:

attr = getattribute(obj, 'bar.name')
# equivalent to:
temp = getattr(obj, 'bar')
attr = getattr(temp, 'name')

Nice_display filter

{% load getter_tags %}
{{ obj|getattribute:column|nice_display }}

For now, all this does is return a comma-separated list of related instances for a many-to-many field.

If the item field has an all method:

return ', '.join(map(unicode, item.all()))

Advanced usage

Export formats

Exports can export to a given format:

class Format(models.Model):
    name = models.CharField(max_length=50)
    file_ext = models.CharField( max_length=10, blank=True)
    mime = models.CharField(max_length=50)
    template = models.CharField(max_length=255)

The mime field is the Content-Type needed for the response. file_ext will be used to compute the export’s filename, provided via Content-Disposition header.

Example: let’s take a naive export to csv:

  • mime: text/csv

  • file_ext: csv

  • name: Naive CSV format

  • template: data_exports/export_detail_csv.html (included as an example)

If an export uses this format, visiting the export’s view page /exports/<export slug> will offer a file download, named <export slug>.csv.

Using your own views

To use your own views, you need to use the same url names as in data_exports/urls.py, and make sure they use the data_exports namespace, as django.core.urlresolvers.reverse is used internally to compute the needed urls.

You can check the included example views in data_exports/views.py, and of course reuse the forms provided in data_exports/forms.py.

Using your own templates

Django-data-exports makes use of Django’s template overloading mechanism. This means that if you provide a data_exports/export_detail.html template which has precedence over the one bundled with the app, it’ll be used.

Example: say you have a templates/ folder in your project, and the appropriate TEMPLATE_DIRS setting. Place your own template in project/templates/data_exports/export_detail.html to have it used instead of the template bundled with the app in data_exports/templates/data_exports/export_detail.html.

There’s three included templates:

  • data_exports/base.html: extended by the two other templates

  • data_exports/export_detail.html: used by default for exports that don’t specify a format

  • data_exports/export_detail_csv.html: used by the “naive csv format” detailed in Export formats.

Hacking

Setup your environment:

git clone https://github.com/magopian/django-data-exports.git
cd django-data-export

Hack and run the tests using Tox to test on all the supported python and Django versions:

make test

To build the docs:

make docs

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-data-exports-0.4.tar.gz (13.3 kB view details)

Uploaded Source

File details

Details for the file django-data-exports-0.4.tar.gz.

File metadata

File hashes

Hashes for django-data-exports-0.4.tar.gz
Algorithm Hash digest
SHA256 7ede8f0d4df72a44d38e9988791e3061c9dcd5cb7e863c44629972fa79198a22
MD5 f56a0e33d53352b520d6be5346d03f21
BLAKE2b-256 cf66745174fd1d8330b979b3efa8bcc0b5c77ac2a415bebf33cded5ddeb35fa1

See more details on using hashes here.

Supported by

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