Sort results when querying objects (such as IP Addresses) via the API #2984

Closed
opened 2025-12-29 18:24:25 +01:00 by adam · 4 comments
Owner

Originally created by @xucito on GitHub (Oct 29, 2019).

Environment

  • NetBox version: v2.3.5

Proposed Functionality

When querying the API, the results should be sortablable via a sort attribute which will sort on the given field name.

This should function similar to the UI interface i.e. /ipam/ip-addresses/?sort=tenant

Order by descending can specified using "-" before the field name.

Use Case

Paging with a sort using the API Directly.

Database Changes

None.

External Dependencies

None.

Originally created by @xucito on GitHub (Oct 29, 2019). <!-- NOTE: This form is only for proposing specific new features or enhancements. If you have a general idea or question, please post to our mailing list instead of opening an issue: https://groups.google.com/forum/#!forum/netbox-discuss NOTE: Due to an excessive backlog of feature requests, we are not currently accepting any proposals which significantly extend NetBox's feature scope. Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * NetBox version: v2.3.5 <!-- Describe in detail the new functionality you are proposing. Include any specific changes to work flows, data models, or the user interface. --> ### Proposed Functionality When querying the API, the results should be sortablable via a sort attribute which will sort on the given field name. This should function similar to the UI interface i.e. <url>/ipam/ip-addresses/?sort=tenant Order by descending can specified using "-" before the field name. <!-- Convey an example use case for your proposed feature. Write from the perspective of a NetBox user who would benefit from the proposed functionality and describe how. ---> ### Use Case Paging with a sort using the API Directly. <!-- Note any changes to the database schema necessary to support the new feature. For example, does the proposal require adding a new model or field? (Not all new features require database changes.) ---> ### Database Changes None. <!-- List any new dependencies on external libraries or services that this new feature would introduce. For example, does the proposal require the installation of a new Python package? (Not all new features introduce new dependencies.) --> ### External Dependencies None.
adam added the type: featurestatus: needs ownerpending closure labels 2025-12-29 18:24:25 +01:00
adam closed this issue 2025-12-29 18:24:25 +01:00
Author
Owner

@kobayashi commented on GitHub (Nov 2, 2019):

Checked this in netbox 2.6.7 also. The straight forward way is just adding filter_backends, ordering_fields described in django-rest-framework, but not seems efficient to implement to all viewsets. 🤔

Any idea for this?

Here is a sample.

class IPAddressViewSet(CustomFieldModelViewSet):
    queryset = IPAddress.objects.prefetch_related(
        'vrf__tenant', 'tenant', 'nat_inside', 'interface__device__device_type', 'interface__virtual_machine',
        'nat_outside', 'tags',
    )
    serializer_class = serializers.IPAddressSerializer
    filterset_class = filters.IPAddressFilter

    # ADD THESE LINES
    filter_backends = [rest_filters.OrderingFilter]
    ordering_fields = ['id', 'vrf__tenant', 'tenant', 'nat_inside', 'interface__device__device_type', 'interface__virtual_machine',
'nat_outside', 'tags',]
@kobayashi commented on GitHub (Nov 2, 2019): Checked this in netbox 2.6.7 also. The straight forward way is just adding `filter_backends`, `ordering_fields` described in [django-rest-framework](https://www.django-rest-framework.org/api-guide/filtering/#orderingfilter), but not seems efficient to implement to all viewsets. :thinking: Any idea for this? Here is a sample. ``` class IPAddressViewSet(CustomFieldModelViewSet): queryset = IPAddress.objects.prefetch_related( 'vrf__tenant', 'tenant', 'nat_inside', 'interface__device__device_type', 'interface__virtual_machine', 'nat_outside', 'tags', ) serializer_class = serializers.IPAddressSerializer filterset_class = filters.IPAddressFilter # ADD THESE LINES filter_backends = [rest_filters.OrderingFilter] ordering_fields = ['id', 'vrf__tenant', 'tenant', 'nat_inside', 'interface__device__device_type', 'interface__virtual_machine', 'nat_outside', 'tags',] ```
Author
Owner

@xucito commented on GitHub (Nov 6, 2019):

Looks that is the easiest way to implement the ordering. You might be able to use the _meta API to get all the fields of the model object you are returning.

https://docs.djangoproject.com/en/2.2/ref/models/meta/#module-django.db.models.options

I do not use django so I just briefly looked at the docs.

@xucito commented on GitHub (Nov 6, 2019): Looks that is the easiest way to implement the ordering. You might be able to use the _meta API to get all the fields of the model object you are returning. https://docs.djangoproject.com/en/2.2/ref/models/meta/#module-django.db.models.options I do not use django so I just briefly looked at the docs.
Author
Owner

@stale[bot] commented on GitHub (May 24, 2020):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our contributing guide.

@stale[bot] commented on GitHub (May 24, 2020): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@stale[bot] commented on GitHub (May 31, 2020):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@stale[bot] commented on GitHub (May 31, 2020): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2984