问题
My Document model has a text_location column, a file path that I need to read from after querying. I am using paginate when querying.
That is created through this code:
class Document(db.Model):
id = db.Column(db.Integer, primary_key=True)
text_location = db.Column(db.String)
page_views = Document.query.paginate(page, 1, False)
I can display {{ page_views }} correctly in the template. However, if I try to open text_location, I get "Pagination" object has no attribute "text_location".
with open(page_views.text_location) as f:
document_text = f.read()
How can I read the text_location for the Documents I queried?
回答1:
Flask-SQLAlchemy's paginate method returns a Pagination object. The list of items retrieved is in the items attribute. Iterate over items to get individual model results.
pager = Document.query.paginate()
for document in pager.items:
with open(document.text_location) as f:
document.text = f.read()
来源:https://stackoverflow.com/questions/49413062/access-a-column-from-a-flask-sqlalchemy-paginate-result