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.6.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

django_data_exports-0.6-py2.py3-none-any.whl (25.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-data-exports-0.6.tar.gz
Algorithm Hash digest
SHA256 7c583154986c51cab4f6345e66d9854438ff72c72dbf96640da710548f66f67d
MD5 c3b68edd981f808b02cbe700cb313104
BLAKE2b-256 462c60ea490b3ea98ea791c357214e171789fac944f2d400c638ca0c51cee690

See more details on using hashes here.

File details

Details for the file django_data_exports-0.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_data_exports-0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e1d013e2daa785341f9a2df96bc20cf10ff0d670e2e74c7737ed980d83108b13
MD5 f81f107663780552fef2b40051e43581
BLAKE2b-256 659af54585f42e2c661935f298176795659ad9342b1a9785e7eaff8870261359

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