[PR #18805] [MERGED] Fixes: #18729 - Reapply model-level ordering on list views (UI and API) to account for annotation #15443

Closed
opened 2025-12-30 00:21:57 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/18805
Author: @bctiemann
Created: 3/4/2025
Status: Merged
Merged: 3/6/2025
Merged by: @arthanson

Base: mainHead: 18729-apply-model-ordering-on-list-views


📝 Commits (1)

  • 4ec1e6f Override get_queryset on generic ObjectListView and NetBoxModelViewSet to reapply model-level ordering

📊 Changes

2 files changed (+10 additions, -0 deletions)

View changed files

📝 netbox/netbox/api/viewsets/__init__.py (+5 -0)
📝 netbox/netbox/views/generic/bulk_views.py (+5 -0)

📄 Description

Fixes: #18729

Adding .annotate to querysets has the effect of negating the ordering setting defined on models. Thus when we put annotations/groupings on the queryset on various list views (for example ClusterViewSet in the API, or ProviderListView in the UI), i.e. to count related objects to show in columns or fields, the model-level sorting is lost and records are returned in an unpredictable order, leading to repetitions and misses by paginating clients.

This change adds a get_queryset override to each of the relevant list view classes, to take the specified queryset and reapply the ordering fields that are defined in the queryset's model class. This occurs centrally and uniformly, thus not requiring many changes throughout the system. It also does not interfere with custom sorting as specified via query params (e.g. &ordering=name).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/18805 **Author:** [@bctiemann](https://github.com/bctiemann) **Created:** 3/4/2025 **Status:** ✅ Merged **Merged:** 3/6/2025 **Merged by:** [@arthanson](https://github.com/arthanson) **Base:** `main` ← **Head:** `18729-apply-model-ordering-on-list-views` --- ### 📝 Commits (1) - [`4ec1e6f`](https://github.com/netbox-community/netbox/commit/4ec1e6f16ee54754d19df5c272860c2132aa2b2c) Override get_queryset on generic ObjectListView and NetBoxModelViewSet to reapply model-level ordering ### 📊 Changes **2 files changed** (+10 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `netbox/netbox/api/viewsets/__init__.py` (+5 -0) 📝 `netbox/netbox/views/generic/bulk_views.py` (+5 -0) </details> ### 📄 Description ### Fixes: #18729 Adding `.annotate` to querysets has the effect of negating the `ordering` setting defined on models. Thus when we put annotations/groupings on the `queryset` on various list views (for example `ClusterViewSet` in the API, or `ProviderListView` in the UI), i.e. to count related objects to show in columns or fields, the model-level sorting is lost and records are returned in an unpredictable order, leading to repetitions and misses by paginating clients. This change adds a `get_queryset` override to each of the relevant list view classes, to take the specified `queryset` and reapply the `ordering` fields that are defined in the queryset's model class. This occurs centrally and uniformly, thus not requiring many changes throughout the system. It also does not interfere with custom sorting as specified via query params (e.g. `&ordering=name`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:21:57 +01:00
adam closed this issue 2025-12-30 00:21:57 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15443