Confused with django model Limit and Offset Implementation

896 views
Skip to first unread message

Swaroop Shankar V

unread,
Jan 22, 2012, 11:40:31 PM1/22/12
to django...@googlegroups.com
Hi,
Am trying to create a custom pagination for my project, but am seriously struck with the django implementation of Limit and offset. I went through the documents, instead of making me understand how it works, i ended up more confused. So suppose I have a page limit of 3 records per page and if i want to load the second set of records how should the following statement be?

Data = NewsFeedStory.objects.order_by('-updated_on')[?:?]

and similarly if i want to load the 3rd set with same page limit, how should the same statement be? I played with the django shell to figure out how this works but am still confused. Please help.

Thanks and Regards,

Swaroop Shankar V

Daniel Roseman

unread,
Jan 23, 2012, 4:32:03 AM1/23/12
to django...@googlegroups.com
I'm not sure why this should confuse you. You seem to have correctly understood the syntax. 

This is just normal Python slicing, which Django transforms into LIMIT and  OFFSET. So, remembering that Python slicing is 0-based and the lower bound is inclusive but the upper is exclusive, the first page is [:3], the second page is [3:6], the third is [6:9], etc.

Note, however, that if you're doing pagination, you should really be using the built-in Paginator class [1], which calculates all this for you given a page number, rather than doing it yourself.

--
DR.

Leandro Ostera Villalva

unread,
Jan 23, 2012, 1:30:41 AM1/23/12
to django...@googlegroups.com
AFAIK it is [offset:page]


Swaroop Shankar V

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Regards,

Leandro Ostera,

Swaroop Shankar V

unread,
Jan 27, 2012, 2:45:07 PM1/27/12
to django...@googlegroups.com
got it working thanks anyway :)

Thanks and Regards,
Swaroop Shankar V



Reply all
Reply to author
Forward
0 new messages