Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient Pagination with Flask and Mongoengine

I have a Flask app with which I would like to display paginated tables of data from a MongoDB Collection. However, there are potentially very many documents in this collection, so I would like to be loading them lazily - only loading the ones which are about to be displayed.

My problem is that on one page in my app, I would like to paginate:

Stuff.objects()

But on different pages I would like to paginate:

Stuff.objects(__raw__=query) or Stuff.objects(message__in=Message.objects(__raw__=query))

Calling any of those particular functions automatically loads all of the relevant objects into memory (as I discovered by running locals()) so I need to paginate the calls with:

Stuff.objects().skip(number).limit(pagelength), or Stuff.objects(__raw__=query).skip(number).limit(pagelength)

So it would seem that I need a Paginator class which I can simply pass Report into, then somehow specify the query information.

Can anyone recommend a solution?

like image 957
Austin R Avatar asked Mar 05 '26 12:03

Austin R


1 Answers

Try using the paginator from flask-mongoengine You can use it like so:

paginator = Pagination(Post.objects, 1, 10)
print paginator.items
like image 163
Ross Avatar answered Mar 08 '26 08:03

Ross