def export_as_csv(admin_model, request, queryset):
<EOL>if getattr(settings, '<STR_LIT>', None):<EOL><INDENT>admin_opts = admin_model.opts<EOL>codename = '<STR_LIT>' % ('<STR_LIT>', admin_opts.object_name.lower())<EOL>has_csv_permission = request.user.has_perm("<STR_LIT>" % (admin_opts.app_label, codename))<EOL><DEDENT>else:<EOL><INDENT>has_csv_permission = admin_model.has_csv_permission(request)if (hasattr(admin_model, '<STR_LIT>') and callable(getattr(admin_model, '<STR_LIT>')))else True<EOL><DEDENT>if has_csv_permission:<EOL><INDENT>opts = admin_model.model._meta<EOL>if getattr(admin_model, '<STR_LIT>', None):<EOL><INDENT>field_names = admin_model.csv_fields<EOL><DEDENT>else:<EOL><INDENT>field_names = [field.name for field in opts.fields]<EOL>field_names.sort()<EOL><DEDENT>if django.VERSION[<NUM_LIT:0>] == <NUM_LIT:1> and django.VERSION[<NUM_LIT:1>] <= <NUM_LIT:5>:<EOL><INDENT>response = HttpResponse(mimetype='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>response = HttpResponse(content_type='<STR_LIT>')<EOL><DEDENT>response['<STR_LIT>'] = '<STR_LIT>' % text(opts).replace('<STR_LIT:.>', '<STR_LIT:_>')<EOL>queryset = queryset.values_list(*field_names)<EOL>pandas.DataFrame(list(queryset), columns=field_names).to_csv(response, index=False, encoding='<STR_LIT:utf-8>')<EOL>return response<EOL><DEDENT>return HttpResponseForbidden()<EOL>
Generic csv export admin action.
based on http://djangosnippets.org/snippets/1697/