Mithril
Mithril

Reputation: 13758

How to paging data in model level (django)

There is a Paginator in django. from django.core.paginator import Paginator. But this Paginator need to get all data to feed it. simple example:

def getMsgByPage():
    page =2

    messages = Message.objects.filter(participation__scene=scene, status='W')

    paginator = Paginator(messages , 50)

    page_obj = paginator.page(page)

    return page_obj 

Every time a user view a page,I should get all messages to generate that.You know,It is very very bad when messages table contain huge data.

So,how can I page messages data in model level? Or I should create a paging stored procedure to do this? But django seems not support stored procedure ...

Upvotes: 4

Views: 363

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Your analysis is wrong. You are not passing the full set of messages to the paginator.

Querysets are lazy, and are not evaluated into they are iterated: in the case of the paginator, it will slice the object - which results in LIMIT/OFFSET being added to the SQL query before it is sent to the db. No more than a page of results will be fetched.

Upvotes: 2

Related Questions