I have a download button on my flask app and i am trying to add functionality that will allow user to download all data from books table locally in csv or excel format.
Another thing i would like to do is to upload excel or csv file and import the data in books table.
For download i have this
@admin_role.route('/download')
@login_required
def post():
si = StringIO()
cw = csv.writer(si)
for book in Book.query.all():
cw.writerows(book)
output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=export.csv"
output.headers["Content-type"] = "text/csv"
return output
But i have error TypeError: writerows() argument must be iterable
this is the model:
class Book(db.Model):
"""
Create a Books table
"""
__tablename__ = 'books'
id = db.Column(db.Integer, primary_key=True)
book_name = db.Column(db.String(60), index=True,unique=True)
author = db.Column(db.String(200), index=True)
quantity = db.Column(db.Integer)
department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
employees_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
publisher = db.Column(db.String(200))
no_of_pgs = db.Column(db.Integer)
pbs_year = db.Column(db.Integer)
genre_id = db.Column(db.Integer, db.ForeignKey('genres.id'), nullable=False)
read = db.Column(db.Enum('NO', 'YES'), default='NO')
borrows = db.relationship('Borrow', backref='book',
lazy='dynamic')