Skip to main content

Class-based view and mixins for handling CSV with Django.

Project description

Building Status

Class-based view and mixins for responding with CSV in Django. django-separated supports Django 1.3+.

Installation

$ pip install django-separated

Documentation

Views

separated.views.CsvView

A ListView that returns a CsvResponse.

You can specify the data for each row using the columns attribute. columns should be an iterable of 2-tuples where the first index is an accessor to get the value off of an object and the second is a column header.

class UserCsvView(CsvView):
    model = User
    columns = [
        ('first_name', 'First name'),
        ('last_name', 'Last name'),
        ('email', 'Email'),
    ]

The accessor can be a string or a callable. If it isn’t a callable, it will be passed into attrgetter to turn into a callable. If the accessor returns a callable, it will be called. All of the following are valid examples of accessors:

  • first_name

  • first_name.upper

  • get_absolute_url

  • lambda x: x.upvotes.count() - x.downvotes.count()

There is a corresponding get_columns method if you need to have more dynamic behavior.

The header index is optional, if you want a header to be generated from the accessor, you can write a simpler columns declaration:

class UserCsvView(CsvView):
    model = User
    columns = [
        'first_name',
        'last_name',
        'email',
    ]

Additionally, you can specify the filename of the CSV file that will be downloaded. It will default to the model name + _list.csv if you don’t provide one. For example:

class UserCsvView(CsvView):
    model = User

will have a filename of user_list.csv. But you can override it by settings the filename attribute. There is a corresponding get_filename that you can override for more complicated behavior.

By default, CsvView will output the headers as the first line. If you want to suppress this behavior, set output_headers to False.

separated.views.CsvResponseMixin

A MultipleObjectMixin subclass that returns a CsvResponse.

This is useful in instances where you want to substitute BaseListView for a ListView of your own. CsvResponseMixin supports all the behavior mentioned in CsvView, the only machinery you need to hook it up is a View class that calls render_to_response with a context that has a queryset available in the object_list key.

class MyWeirdBaseListView(View):
    def get(self, request, *args, **kwargs):
        return self.render_to_response({
            'object_list': User.objects.all(),
        })

class MyWeirdCsvView(CsvResponseMixin, MyWeirdBaseListView):
    pass

separated.views.CsvResponse

A subclass of HttpResponse that will download as CSV. CsvResponse requires a filename as the first argument of the constructor.

Getters

django-separated provides a couple of helpers for normalizing the data that comes off of the model before sending it to the CSV writer. These are all based on a Getter class which handles the different types of accessors.

separated.utils.BooleanGetter

If you have a boolean value that you wish to be transformed into Yes or No, you can use the BooleanGetter:

from separated.utils import BooleanGetter

class UserCsvView(CsvView):
    model = User
    columns = [
        BooleanGetter('is_admin'),
    ]

separated.utils.DisplayGetter

If you have a model field that has choices and you want the human readable display to appear in the CSV, you can use the DisplayGetter:

from separated.utils import BooleanGetter

class User(models.Model):
    favorite_color = models.CharField(max_length=255,
        choices=(
            ('blue', 'Blue'),
            ('green', 'Green'),
            ('red', 'Red'),
        ))

class UserCsvView(CsvView):
    model = User
    columns = [
        DisplayGetter('favorite_color'),
    ]

This will end up using the get_favorite_color_display method that Django automatically adds.

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-separated-0.9.0.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file django-separated-0.9.0.tar.gz.

File metadata

File hashes

Hashes for django-separated-0.9.0.tar.gz
Algorithm Hash digest
SHA256 045265d669c903790005b0e86bed7c1e951585f10bbed22faf03cfc74456f705
MD5 e445d8175c24d46da2d8fd72e29aa6ad
BLAKE2b-256 8e75970ce5f1781e2c42acb8fcd8874d262b92e5a33dbf86e218d88721fab5ae

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