0

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

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

Sorry for the confusion, but the main question is "How to add literal_column() (with the value of num_pages and/or total_count) to the resulting QuerySet?"
Why do you need this as a separate column? The paginate function already returns the number of pages in the query set.
My code returns (return json.dumps(records, cls=AlchemyEncoder)) json representation of a paginate().items query set, and i want num_pages&total_count to be a part of this json data, so that i could return all info (db rows + a couple of summary values) within a single http response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.