2

According to the documentation, nothing special is required to enable pagination when using a class inheriting from GenericAPIView.

I've looked in the code of django and django rest framework and the ViewSet queryset doesn't seem ordered. The pagination only adds the SQL keywords LIMIT and OFFSET to the query.

What I don't understand is how is handled a queryset that doesn't have a deterministic order ? Why the documentation doesn't say to add a .order_by() statement at the end of the ViewSet queryset ?

oliparcol
  • 23
  • 5
  • Probably just a flaw in the documentation, although many databases have a consistent order across queries - unfortunately it is not the case with postgres, the ordering tends to change when you update the table. – Paulo Scardine Feb 17 '17 at 20:16
  • 1
    In that case, this is a big flaw as it is never mentioned that the viewset queryset must be ordered when using the `PageNumberPagination`. Moreover, when using the `CursorPagination` an order is forced on the queryset by DRF. – oliparcol Feb 17 '17 at 21:03
  • See https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/pagination.py#L467 – oliparcol Feb 17 '17 at 21:30

2 Answers2

0

Queries without explicit order should be database specific and there is no guarantee you will get a sorted result set (unless django sorts by id as default), please see here for similar answer.

Community
  • 1
  • 1
hurturk
  • 5,214
  • 24
  • 41
  • 1
    I agree with you, this is why I'm surprised that it is not necessary to force an `order_by` on the queryset when using this type of pagination. – oliparcol Feb 17 '17 at 21:07
0

Ordering is required to have pagination working properly. Without that you can have few rows repeated across different pages and few missing. It is also mentioned in django docs:

For consistent pagination, QuerySets should be ordered, e.g. with an order_by() clause or with a default ordering on the model.

https://docs.djangoproject.com/en/2.2/topics/pagination/#required-arguments

It is also worth to read this article about default ordering: https://learn.microsoft.com/pl-pl/archive/blogs/conor_cunningham_msft/no-seatbelt-expecting-order-without-order-by

Kasikn77
  • 199
  • 1
  • 8