Created
March 27, 2015 00:05
-
-
Save mrjoes/e563e70e2e0b28117ef4 to your computer and use it in GitHub Desktop.
CSV export for Flask-Admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseModelView(ModelView): | |
list_template = 'admin/model_list.html' | |
export_columns = None | |
# Exporting | |
def _get_data_for_export(self): | |
view_args = self._get_list_extra_args() | |
# Map column index to column name | |
sort_column = self._get_column_by_idx(view_args.sort) | |
if sort_column is not None: | |
sort_column = sort_column[0] | |
_, query = self.get_list(view_args.page, sort_column, view_args.sort_desc, view_args.search, | |
view_args.filters, execute=False) | |
return query.limit(None).all() | |
def get_export_csv(self): | |
self.export_columns = self.export_columns or [column_name for column_name, _ in self._list_columns] | |
io = StringIO() | |
rows = csv.DictWriter(io, self.export_columns) | |
data = self._get_data_for_export() | |
rows.writeheader() | |
for item in data: | |
row = {column: unicode(rec_getattr(item, column)).encode('utf-8') for column in self.export_columns} | |
rows.writerow(row) | |
io.seek(0) | |
return io.getvalue() | |
@expose('/export/') | |
def export(self): | |
response = make_response(self.get_export_csv()) | |
response.mimetype = 'text/csv' | |
response.headers['Content-Disposition'] = 'attachment; filename=%s.csv' % self.name.lower().replace(' ', '_') | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2020: Set
BaseModelView.can_export = True