Model mixin to return all field data in a dictionary
Get all field data, e.g. for an export.
- models
- model
- export
- orm
- metaprogramming
Get all field data, e.g. for an export.
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`
How to use =========== Save the snippet to a file utils.py, and add the following view to your Django app: from django.http import HttpResponse from .utils import queryset_to_workbook def download_workbook(request): queryset = User.objects.all() columns = ( 'first_name', 'last_name', 'email', 'is_staff', 'groups') workbook = queryset_to_workbook(queryset, columns) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="export.xls"' workbook.save(response) return response Note: you can use dotted notation (`'foreign_key.foreign_key.field'`) in the columns parameter to access fields that are accessible through the objects returned by the queryset (in that case you probably want to use `select_related` with your queryset).
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 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 [Snippet 2558](http://djangosnippets.org/snippets/2558/) but without saving the generated file to disk before downloading. Requires, pyExcelerator Usage: Add the code to your project, e.g. a file called actions.py in the project root. Register the action in your apps admin.py: `from myproject.actions import export_as_xls class MyAdmin(admin.ModelAdmin): actions = [export_as_xls]`
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.
Based on [http://djangosnippets.org/snippets/1697/](http://djangosnippets.org/snippets/1697/) but improved with multiline fields, and unicode chars. Also it generates an xls file, not a csv one. Requires, pyExcelerator *Usage:* Add the code to your project, e.g. a file called actions.py in the project root. Register the action in your apps admin.py: from myproject.actions import export_as_xls class MyAdmin(admin.ModelAdmin): actions = [export_as_xls]
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
Just like it says -- set it up and run. Use it for server migrations, for project handoffs, in cron jobs, you name it. I have never had problems exporting models to individual fixtures in this way, and only one bout of trouble re-importing them (and that was, like, an impossible-triangle of OneToOneField dependencies anyway, and they were going from a sqlite file to a postgres schema that totally had inappropriate nullable columns in it). I find that the json files named for what they contain is helpful when and if manage.py does freak out during an import, as the output from `loaddata` command is so opaque it's autistic, basically. A trivial refactoring effort could make it into a management command -- it already makes use of the builtin `dumpdata` command class internally. However I did not feel like overthinking it enough to set it up in a repository (doubtlessly padded with unrelated 'utilities' and explanatory .rst files) and then writing a blog post to sell it to you. That is why you are reading this code here, instead of on GitHub. Don't get me wrong, GitHub is awesome, and like a consummate host... but not the way I love me some quick and dirty snippet code, these days. Whatever, you say lazy, I say productively relaxed, potato/potahto. Erm. In any case please do enjoy this model fixture-exporter. Yes.
This is a basic view for exporting data from models. It is designed so that you can provide the GET variables used for searching/filtering within listdisplay pages in the admin, and the code will filter the same way. It allows ouputting of model data in various formats using serializers. This should be used as a urlpattern through get_url, so that you can use the admin_view decorator to properly secure the data.
This is a basic model view for exporting data from models. It is designed so that you can provide the GET variables used for searching/filtering within listdisplay pages in the admin, and the code will filter the same way. It allows the output of model data in various formats using serializers or templates.
23 snippets posted so far.