user4700847
user4700847

Reputation: 164

Access a column from a Flask-SQLAlchemy paginate result

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?

Upvotes: 1

Views: 1966

Answers (1)

davidism
davidism

Reputation: 127310

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()

Upvotes: 3

Related Questions