Question

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 ...

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top