Streaming large CSV files via admin actions
Based on https://djangosnippets.org/snippets/2020/ and https://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django Can be used on really large querysets.
- admin
- csv
- admin-actions
Based on https://djangosnippets.org/snippets/2020/ and https://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django Can be used on really large querysets.
FileField that checks that the file is a valid CSV and if specified in `expected_fieldnames` checks that the fields match exactly. The widget's `accept` parameter is set to accept csv, text and excel files. **TODO**: Validate the entirety of the CSV file, not just the headers. But this should be enough for most use cases, as checking the whole file could be computationally expensive for huge files. Example usage: people = CSVField(expected_fieldnames=['First Name', 'Last Name'])
This snippets is inspired from [#2995](https://djangosnippets.org/snippets/2995/) but add the following: * get rid of `singledispatch` so we can use it with python2 without pip installing anything * streaming capabilities * the configuration params (header, fields, exclude...) are passed to the action function so we don't pollute the ModelAdmin class * fix utf8 issue in header
Save the snippet as actions.py within your core app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: `from actions import export_as_csv_action` `class YourModelAdmin(admin.ModelAdmin):` ` list_display = ('field', 'get_field2')` ` actions = [export_to_csv(filename='your-model')]` ` def get_field2(self, obj):` ` return obj.field2`
Based on [#2868](https://djangosnippets.org/snippets/2868/) Now with support for writing the absolute URL of file fields
Based on [#2369](https://djangosnippets.org/snippets/2369/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])] - Unicode fix (requires unidecode)
This owes a debt to a number of earlier snippets by myself and others, including: *(most directly)* [#2868](http://djangosnippets.org/snippets/2868/), plus [#2020](http://djangosnippets.org/snippets/2020/), [#2712](http://djangosnippets.org/snippets/2712/), [#1697](http://djangosnippets.org/snippets/1697/) Use of OrderedDict means it requires Python 2.7+. You also need to `pip install singledispatch` which is a backport of a Python 3.4 feature. Singledispatch (along with custom attributes instead of a factory function) gives a very clean interface in your ModelAdmin, whether or not you need a custom `short_description`. This version allows you to (optionally) specify custom column labels, or to (optionally) use Django's usual prettifying mechanism (`field.verbose_name`). Thanks to #2868 it can do relation-spanning double-underscore lookups and also model attribute/method (rather than field) lookups for columns, just like you can in your ModelAdmin `list_display`.
based on #2020 This one is even more generic than the previous generic ones since you can specify in fields any attribute you will give to the admin interface and not just fields from the model. You can for example directly export the list_display as a list of fields, including look-ups for related attributes that you may have defined in a function inside the Admin Class
This is a management command to export an app to csv files, or import from csv files. Code originally from django-csvimport
Based on [#2712](../2712/) "This snippet creates a simple generic export to csv action that you can specify the fields you want exported and the labels used in the header row for each field. It expands on #2020 by using list comprehensions instead of sets so that you also control the order of the fields as well." The additions here allow you to span foreign keys in the list of field names, and you can also reference callables.
Based on [#2020](http://djangosnippets.org/snippets/2020/) This snippet creates a simple generic export to csv action that you can specify the fields you want exported and the labels used in the header row for each field. It expands on #2020 by using list comprehensions instead of sets so that you also control the order of the fields as well.
This function downloads selected rows to CSV file. The snippet is based on snippet 1697 and 2020. This function downloads all columns given in the list_display, including callable items.
Based on [#2020](/snippets/2020/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])] * - Added UTF-8 encoding support * - Bugs fixed
CSV serialization for models. Can be used via the dumpdata/loaddata management commands or programmatically using the django.core.serializers module. Supports multiple header lines and natural keys. Add the following to settings.py: SERIALIZATION_MODULES = { 'csv' : 'path.to.csv_serializer', } Examples of usage: $ python manage.py dumpdata --format csv auth.user > users.csv from django.core import serializers csvdata = serializers.serialize('csv', Foo.objects.all()) To run the regression tests distributed with the Django tarball: $ cd /path/to/Django-1.2.x/tests $ PYTHONPATH=/path/to/myproject ./runtests.py --settings=myproject.settings serializers_regress
**I've since made a better snippet for this: [#2995](http://djangosnippets.org/snippets/2995/)** based on [#1697](http://djangosnippets.org/snippets/1697/) This one is even more generic since you can specify which fields to include or exclude, a custom description text for the drop-down menu and whether to output the header row.
30 snippets posted so far.