mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-22 13:22:24 -06:00
#303: First stab at implementing a natural ordering for sites, racks, and devices
This commit is contained in:
30
netbox/utilities/managers.py
Normal file
30
netbox/utilities/managers.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from django.db.models import Manager
|
||||
|
||||
|
||||
class NaturalOrderByManager(Manager):
|
||||
|
||||
def natural_order_by(self, *fields):
|
||||
"""
|
||||
Attempt to order records naturally by segmenting a field into three parts:
|
||||
|
||||
1. Leading integer (if any)
|
||||
2. Middle portion
|
||||
3. Trailing integer (if any)
|
||||
|
||||
:param fields: The fields on which to order the queryset. The last field in the list will be ordered naturally.
|
||||
"""
|
||||
db_table = self.model._meta.db_table
|
||||
primary_field = fields[-1]
|
||||
|
||||
id1 = '_{}_{}1'.format(db_table, primary_field)
|
||||
id2 = '_{}_{}2'.format(db_table, primary_field)
|
||||
id3 = '_{}_{}3'.format(db_table, primary_field)
|
||||
|
||||
queryset = super(NaturalOrderByManager, self).get_queryset().extra(select={
|
||||
id1: "CAST(SUBSTRING({}.{} FROM '^(\d+)') AS integer)".format(db_table, primary_field),
|
||||
id2: "SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')".format(db_table, primary_field),
|
||||
id3: "CAST(SUBSTRING({}.{} FROM '(\d+)$') AS integer)".format(db_table, primary_field),
|
||||
})
|
||||
ordering = fields[0:-1] + (id1, id2, id3)
|
||||
|
||||
return queryset.order_by(*ordering)
|
||||
Reference in New Issue
Block a user