photocrowd / django-cursor-pagination

Cursor-based pagination for Django
BSD 3-Clause "New" or "Revised" License
156 stars 27 forks source link

Consider using the offset approach used by DRF #2

Closed mjtamlyn closed 8 years ago

mjtamlyn commented 8 years ago

In this project at the moment I have not used the offset approach which used by Django rest framework and inspired by David Cramer's blog post. The main reason for this is that it doesn't provide "globally unique" cursors. In particular you cannot guarantee that before=FIRST_ELEMENT_CURSOR will give the correct data set, as the offset of the first element cannot be calculated. This edge case could possibly be covered by a second query checking for uniqueness on that value, although this would be overkill in most situations.

From a bit of research, Disqus is the only implementation of cursor pagination "in the wild" which makes use of offsets. Both twitter and facebook use id or timestamp based approaches. By encoding the value of all fields used in the ordering into the cursor (rather than just the first in DRF's implementation), we don't need offsets and the resulting code is much simpler - in particular when calculating the cursor for every item on the page.

I've always tried to make sure that any paginated set has a truly unique ordering - for most applications either id or created is sufficient. It also means you can do nice things like .order_by('-score', '-created') where score may not be unique enough to guarantee that you don't need large offsets.

Thoughts?