Unverified Commit ce6e6b48 authored by Serge S. Koval's avatar Serge S. Koval Committed by GitHub

Merge pull request #1733 from flask-admin/export-example

Add csv and xls export to example app
parents 203291f7 35c12509
......@@ -43,6 +43,9 @@ class User(db.Model):
def __str__(self):
return "{}, {}".format(self.last_name, self.first_name)
def __repr__(self):
return "{}: {}".format(self.id, self.__str__())
class Pet(db.Model):
id = db.Column(db.Integer, primary_key=True)
......@@ -187,9 +190,10 @@ class UserAdmin(sqla.ModelView):
# Customized Post model admin
class PostAdmin(sqla.ModelView):
column_exclude_list = ['text']
column_list = ['id', 'user', 'title', 'date', 'tags']
column_default_sort = ('date', True)
column_sortable_list = [
'id',
'title',
'date',
('user', ('user.last_name', 'user.first_name')), # sort on multiple columns
......@@ -208,6 +212,9 @@ class PostAdmin(sqla.ModelView):
'tags',
filters.FilterLike(Post.title, 'Fixed Title', options=(('test1', 'Test 1'), ('test2', 'Test 2'))),
]
can_export = True
export_max_rows = 1000
export_types = ['csv', 'xls']
# Pass arguments to WTForms. In this case, change label for text field to
# be 'Big Text' and add required() validator.
......
Flask
Flask-Admin
Flask-SQLAlchemy
tablib
......@@ -2330,12 +2330,12 @@ class BaseModelView(BaseView, ActionsMixin):
if encoding:
mimetype = '%s; charset=%s' % (mimetype, encoding)
ds = tablib.Dataset(headers=[c[1] for c in self._export_columns])
ds = tablib.Dataset(headers=[csv_encode(c[1]) for c in self._export_columns])
count, data = self._export_data()
for row in data:
vals = [self.get_export_value(row, c[0]) for c in self._export_columns]
vals = [csv_encode(self.get_export_value(row, c[0])) for c in self._export_columns]
ds.append(vals)
try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment