Skip to content
Browse files

quick hack - export CSV according to current change_list filters (i.e…

…. what you see is what you get exported)
  • Loading branch information...
1 parent 4f559b5 commit 4d766b8b07bf91cf6c72b5fd55dff24e15892d55 @vvarp vvarp committed with
Showing with 8 additions and 2 deletions.
  1. +7 −1 csvexport/admin.py
  2. +1 −1 csvexport/templates/csvexport/change_list.html
View
8 csvexport/admin.py
@@ -38,11 +38,17 @@ def wrapper(*args, **kwargs):
def csv_export(self, request):
fields = self.get_csv_export_fields(request)
headers = [self.csv_get_fieldname(f) for f in fields]
+
+ from django.contrib.admin.views.main import ChangeList
+ cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,
+ self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
+ qs = cl.get_query_set()
+
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % self.csv_get_export_filename(request)
writer = csv.writer(response, self.csv_export_dialect, **self.csv_export_fmtparam)
writer.writerow(headers)
- for row in self.model.objects.all():
+ for row in qs:
csvrow = [f.encode('utf-8') if isinstance(f, unicode) else f for f in [self.csv_resolve_field(row, f) for f in fields]]
writer.writerow(csvrow)
return response
View
2 csvexport/templates/csvexport/change_list.html
@@ -4,7 +4,7 @@
{% block object-tools %}
<ul class="object-tools">
- <li><a href="{{ csv_export_url }}" class="addlink">{% trans "Export as CSV" %}</a></li>
+ <li><a href="{{ csv_export_url }}?{% for k,v in request.GET.items %}{{k}}={{v}}{% if not forloop.last %}&{% endif %}{% endfor %}" class="addlink">{% trans "Export as CSV" %}</a></li>
{% if has_add_permission %}
<li>
<a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">

0 comments on commit 4d766b8

Please sign in to comment.
Something went wrong with that request. Please try again.