Extend the use of natural ordering to additional models #7403

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

Originally created by @JohannesKreuzer on GitHub (Dec 21, 2022).

Originally assigned to: @arthanson on GitHub.

NetBox version

v3.4.1

Feature type

Data model extension

Proposed functionality

Extend Virtualisation Cluster model to use natualsorting by the cluster name.

Use case

Better sorting of the clusters without padding the numbers in the name.

Database changes

No response

External dependencies

No response

Originally created by @JohannesKreuzer on GitHub (Dec 21, 2022). Originally assigned to: @arthanson on GitHub. ### NetBox version v3.4.1 ### Feature type Data model extension ### Proposed functionality Extend Virtualisation Cluster model to use natualsorting by the cluster name. ### Use case Better sorting of the clusters without padding the numbers in the name. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: featurecomplexity: mediumnetbox labels 2025-12-29 20:22:59 +01:00
adam closed this issue 2025-12-29 20:23:00 +01:00
Author
Owner

@jeremystretch commented on GitHub (Dec 29, 2022):

IMO it doesn't make sense to do this only for clusters as a discrete goal. For instance, #10644 proposes also implementing natural ordering for circuits.

We use natural ordering for a few models in NetBox currently; namely sites, racks, devices, device components, VMs, and VM interfaces. This is accomplished by including a "hidden" _name field which stores a naturalized representation of the object's name suitable for database ordering.

While I'm not opposed to enabling natural ordering on other models, if we decide to do so, we should:

  1. Re-evaluate the current implementation to determine if a better approach exists
  2. Identify all suitable models and implement the change on all
  3. Ensure that the implementation is scalable (e.g. via a mixin class vs. explicit field definitions as we do currently)
@jeremystretch commented on GitHub (Dec 29, 2022): IMO it doesn't make sense to do this only for clusters as a discrete goal. For instance, #10644 proposes also implementing natural ordering for circuits. We use [natural ordering](https://stackoverflow.com/questions/5167928/what-is-natural-ordering-when-we-talk-about-sorting) for a few models in NetBox currently; namely sites, racks, devices, device components, VMs, and VM interfaces. This is accomplished by including a "hidden" `_name` field which stores a naturalized representation of the object's `name` suitable for database ordering. While I'm not opposed to enabling natural ordering on other models, if we decide to do so, we should: 1. Re-evaluate the current implementation to determine if a better approach exists 2. Identify _all_ suitable models and implement the change on all 3. Ensure that the implementation is scalable (e.g. via a mixin class vs. explicit field definitions as we do currently)
Author
Owner

@kkthxbye-code commented on GitHub (Dec 30, 2022):

  • Re-evaluate the current implementation to determine if a better approach exists

Maybe we can use postgres collation as there is support for natural ordering.

We would have to create the collation in a migration with raw sql I assume, like:

CREATE COLLATION natural_sort (provider = icu, locale = 'en@colNumeric=yes');

And then the collation can be specified on the field like:

name = models.CharField(
    max_length=100,
    db_collation='numeric'
)

It also seems possible to specify it at query time.

@kkthxbye-code commented on GitHub (Dec 30, 2022): > * Re-evaluate the current implementation to determine if a better approach exists Maybe we can use postgres collation as there is support for natural ordering. We would have to create the collation in a migration with raw sql I assume, like: ```SQL CREATE COLLATION natural_sort (provider = icu, locale = 'en@colNumeric=yes'); ``` And then the collation can be specified on the field like: ```python name = models.CharField( max_length=100, db_collation='numeric' ) ``` It also seems possible to specify it at query time.
Author
Owner

@github-actions[bot] commented on GitHub (Apr 6, 2023):

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. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Apr 6, 2023): 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. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@jeremystretch commented on GitHub (May 2, 2023):

Collation definitely seems like the path forward, if it proves feasible. I recall going down this road a bit in the past but ran into limitations on older PostgreSQL versions. Django 4.2 drops support for PostgreSQL 11 (see #12237), so maybe that will free us up some?

@jeremystretch commented on GitHub (May 2, 2023): Collation definitely seems like the path forward, if it proves feasible. I recall going down this road a bit in the past but ran into limitations on older PostgreSQL versions. Django 4.2 drops support for PostgreSQL 11 (see #12237), so maybe that will free us up some?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7403