Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin - export as csv

I am using the following Django Snippet to export the results of the admin change_list page to csv http://djangosnippets.org/snippets/790/

I have this working but currently I can only get it to export the fields which are defined in the list_display variable.

The function accepts a list (named as fields, see below) as a variable which defines which fields you want to export but I can't work out how to pass this into the function. This is because the function is called using the url pattern which does not include a fields variable.

def admin_list_export(request, model_name, app_label, queryset=None, fields=None, list_display=True): 
like image 941
John Avatar asked Nov 22 '10 12:11

John


1 Answers

You should be able to pass parameters to the function in the url definition:

(r'^admin/(?P<app_label>[\d\w]+)/(?P<model_name>[\d\w]+)/csv/',\    
 'util.csv_view.admin_list_export', {'fields': ['myfield1', 'myfield2']}),

You can also pass list_display = False to the function and it will ignore the list_display setting on your ModelAdmin and use all fields the model has!

like image 130
Bernhard Vallant Avatar answered Oct 14 '22 22:10

Bernhard Vallant