开发者

django admin - export as csv

开发者 https://www.devze.com 2023-01-26 21:13 出处:网络
I am using the following Django Snippet to export the res开发者_如何学Pythonults of the admin change_list page to csv http://djangosnippets.org/snippets/790/

I am using the following Django Snippet to export the res开发者_如何学Pythonults 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): 


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消