Ordering of custom fields does not work from the API #9111

Closed
opened 2025-12-29 20:45:33 +01:00 by adam · 4 comments
Owner

Originally created by @Yutsuten on GitHub (Jan 19, 2024).

Deployment Type

Self-hosted

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

From an empty NetBox instance:

  1. Create a custom field with Content Type of "DCIM > Device", Name of "foo" (can be anything) and Type of "Text" (the default)
  2. Create a Device (for that a site, device role, manufacturer and device type are needed. Randomly create one of each)

1st device: Name = server1; foo = 2
2nd device: Name = server2; foo = 1
3rd device: Name = server3; foo = 3

Then access the API on /api/dcim/devices/?ordering=cf_foo or /api/dcim/devices/?ordering=-cf_foo.

Expected Behavior

I expect the results to be ordered by the custom field foo.

  • When using ordering=cf_foo, I expect server2 server1 server3
  • When using ordering=-cf_foo, I expect server3 server1 server2

Observed Behavior

Both ordering=cf_foo and ordering=-cf_foo return the results in the same order: server1 server2 server3

NetBox REST API Ordering Custom Field Not Working


From what I've read on #10721, written by @arthanson

(...) The issue here is that for object-type custom fields the ID of the object is stored so it is basically a generic-foreign-key, so the sorting is happening on the ID (which would be the creation order). It would not be straight-forward to de-reference this to the object and sort on the appropriate sub-field in a performant way for the list. After discussion we will probably look at disabling sorting on this if the custom-field is an object or multi-object type.

From what I understand, sorting anything that isn't object or multi-object type should be possible. ebf6ce1b01/netbox/netbox/tables/columns.py (L459-L469)

Hopefully I'm not missing anything here. Very sorry if that's the case. I've been expending hours on this already and don't know what's wrong here. Looks like a bug to me.

Thank you in advance!

Originally created by @Yutsuten on GitHub (Jan 19, 2024). ### Deployment Type Self-hosted ### NetBox Version v3.7.1 ### Python Version 3.11 ### Steps to Reproduce From an empty NetBox instance: 1. Create a custom field with Content Type of "DCIM > Device", Name of "foo" (can be anything) and Type of "Text" (the default) 2. Create a Device (for that a site, device role, manufacturer and device type are needed. Randomly create one of each) 1st device: Name = server1; foo = 2 2nd device: Name = server2; foo = 1 3rd device: Name = server3; foo = 3 Then access the API on `/api/dcim/devices/?ordering=cf_foo` or `/api/dcim/devices/?ordering=-cf_foo`. ### Expected Behavior I expect the results to be ordered by the custom field foo. - When using `ordering=cf_foo`, I expect `server2 server1 server3` - When using `ordering=-cf_foo`, I expect `server3 server1 server2` ### Observed Behavior Both `ordering=cf_foo` and `ordering=-cf_foo` return the results in the same order: `server1 server2 server3` ![NetBox REST API Ordering Custom Field Not Working](https://github.com/netbox-community/netbox/assets/7322925/db22bec0-3962-4dc3-8798-f60237afd54b) --- From what I've read on #10721, written by @arthanson > (...) The issue here is that for object-type custom fields the ID of the object is stored so it is basically a generic-foreign-key, so the sorting is happening on the ID (which would be the creation order). It would not be straight-forward to de-reference this to the object and sort on the appropriate sub-field in a performant way for the list. After discussion we will probably look at disabling sorting on this if the custom-field is an object or multi-object type. From what I understand, sorting anything that isn't object or multi-object type **should** be possible. https://github.com/netbox-community/netbox/blob/ebf6ce1b01b48253c4396fd2c102ca04dc399979/netbox/netbox/tables/columns.py#L459-L469 Hopefully I'm not missing anything here. Very sorry if that's the case. I've been expending hours on this already and don't know what's wrong here. Looks like a bug to me. Thank you in advance!
adam added the type: bug label 2025-12-29 20:45:33 +01:00
adam closed this issue 2025-12-29 20:45:34 +01:00
Author
Owner

@markkuleinio commented on GitHub (Jan 19, 2024):

I've been expending hours on this already

Workaround: Since this is API-related, it is usually trivial to do the sorting on the consumer side.

@markkuleinio commented on GitHub (Jan 19, 2024): > I've been expending hours on this already Workaround: Since this is API-related, it is usually trivial to do the sorting on the consumer side.
Author
Owner

@Yutsuten commented on GitHub (Jan 19, 2024):

In my real use case, I need to do pagination. There are thousands of devices than in this example.
Retrieving all the data and doing the sort on the client (consumer) side is not a viable solution.

@Yutsuten commented on GitHub (Jan 19, 2024): In my real use case, I need to do pagination. There are thousands of devices than in this example. Retrieving all the data and doing the sort on the client (consumer) side is not a viable solution.
Author
Owner

@markkuleinio commented on GitHub (Jan 19, 2024):

Sure, that's one thing. It took 150 MB of memory for me to fetch and sort 13000+ prefixes in Python

@markkuleinio commented on GitHub (Jan 19, 2024): Sure, that's one thing. It took 150 MB of memory for me to fetch and sort 13000+ prefixes in Python
Author
Owner

@jeffgdotorg commented on GitHub (Jan 19, 2024):

Thanks for reporting this issue.

The ordering facility in the API is not meant to be exhaustive, but to provide coverage of higher-level attributes such as name, site, and location. The Ordering Objects section of the docs could admittedly be clearer on this point.

I'm closing this issue because it's not really a bug. Please create a new feature request issue describing the desired functionality and outlining a concrete use case and field name (foo is a classic, but it doesn't convey the actual problem you're trying to solve) and we'll take a fresh look at it.

@jeffgdotorg commented on GitHub (Jan 19, 2024): Thanks for reporting this issue. The ordering facility in the API is not meant to be exhaustive, but to provide coverage of higher-level attributes such as name, site, and location. The [Ordering Objects](https://docs.netbox.dev/en/stable/reference/filtering/#ordering-objects) section of the docs could admittedly be clearer on this point. I'm closing this issue because it's not really a bug. Please create a new feature request issue describing the desired functionality and outlining a concrete use case and field name (`foo` is a classic, but it doesn't convey the actual problem you're trying to solve) and we'll take a fresh look at it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9111