AlexMe
AlexMe

Reputation: 96

Adding literal_column to Flask-SQLAlchemy query

I'm trying to add some scalar value column to the selection that uses Flask-SQLAlchemy pagination functionality. Currently i have:

records = Item.query.paginate(1, 3, False).items

How do i have to edit this code to add column that contains total number of pages (and ideally one more column with total records number)?

Upvotes: 0

Views: 403

Answers (2)

pjcunningham
pjcunningham

Reputation: 8046

You could try passing passing a dictionary to json.dumps:

paginated = Item.query.paginate(1, 3, False)
results = {
    "records": paginated.items,
    "num_pages": paginated.pages,
    "total_count": paginated.total   
}

return json.dumps(results, cls=AlchemyEncoder)

Upvotes: 1

jumbopap
jumbopap

Reputation: 4147

paginate returns a Pagination object which contains attributes like pages, and total. See the API.

paginated = Item.query.paginate(1, 3, False)
num_pages = paginated.pages # gives you total number of pages
total_count = paginated.total

Upvotes: 0

Related Questions