Skip to content

Commit 5c2290d

Browse files
author
Carlton Gibson
authored
Add note on not using floats with CursorPagination (#5459)
Closes #5160, closes #5164.
1 parent 760268a commit 5c2290d

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

docs/api-guide/pagination.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ Proper usage of cursor pagination should have an ordering field that satisfies t
179179
* Should be an unchanging value, such as a timestamp, slug, or other field that is only set once, on creation.
180180
* Should be unique, or nearly unique. Millisecond precision timestamps are a good example. This implementation of cursor pagination uses a smart "position plus offset" style that allows it to properly support not-strictly-unique values as the ordering.
181181
* Should be a non-nullable value that can be coerced to a string.
182+
* Should not be a float. Precision errors easily lead to incorrect results.
183+
Hint: use decimals instead.
184+
(If you already have a float field and must paginate on that, an
185+
[example `CursorPagination` subclass that uses decimals to limit precision is available here][float_cursor_pagination_example].)
182186
* The field should have a database index.
183187

184188
Using an ordering field that does not satisfy these constraints will generally still work, but you'll be losing some of the benefits of cursor pagination.
@@ -317,3 +321,4 @@ The [`django-rest-framework-link-header-pagination` package][drf-link-header-pag
317321
[drf-proxy-pagination]: https://github.com/tuffnatty/drf-proxy-pagination
318322
[drf-link-header-pagination]: https://github.com/tbeadle/django-rest-framework-link-header-pagination
319323
[disqus-cursor-api]: http://cramer.io/2011/03/08/building-cursors-for-the-disqus-api
324+
[float_cursor_pagination_example]: https://gist.github.com/keturn/8bc88525a183fd41c73ffb729b8865be#file-fpcursorpagination-py

0 commit comments

Comments
 (0)