Skip to content

Fixes #18900: raise QuerySetNotOrdered exception when trying to paginate unordered API querysets #19943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2025

Conversation

jnovinger
Copy link
Member

Fixes: #18900

  • Introduce and raise QuerySetNotOrdered exception when an API endpoint tries to paginate a queryset instance that has not been ordered or has lost its ordering (see Netbox API returns duplicate resources during paging with offset #18729).
  • Modify reapply_model_ordering() to not reapply default orderings if queryset is already ordered.
  • Provide default ordering behavior for users.Token model
  • Provide default ordering behavior for Tagged Item API list endpoint (/api/extras/tagged-objects/). See specific commit message for reasoning of this approach.

Defines a new exception, `QuerySetNotOrdered`, and raises it in
`OptionalLimitOffsetPagination.paginate_queryset` in the right
conditions:
- the iterable to be paginated is a QuerySet isinstance
- the `queryset.ordered` flag is not truthy
I chose to implement this here for TaggedItemViewSet, rather than on the
model, because any meaningful ordering is going to be done on the
related Tag instance and I didn't want to introduce potential, not well
understood side-effects by applying a model-wide ordering via a related
model field.
@jnovinger jnovinger requested review from a team and bctiemann and removed request for a team July 23, 2025 17:37
@jnovinger jnovinger marked this pull request as draft July 23, 2025 20:54
@jnovinger jnovinger marked this pull request as ready for review July 29, 2025 16:18
bctiemann
bctiemann previously approved these changes Jul 29, 2025
@jnovinger jnovinger requested a review from bctiemann July 29, 2025 16:36
@jnovinger jnovinger merged commit c736ce3 into main Jul 29, 2025
10 checks passed
@jnovinger jnovinger deleted the 18900-paginator-should-not-paginate-unsorted-queryset branch July 29, 2025 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The REST API paginator should raise an exception if attempting to paginate an unordered queryset
2 participants