mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Standardize usage of NaturalOrderingManager
This commit is contained in:
@@ -1,26 +1,30 @@
|
||||
from django.db.models import Manager
|
||||
|
||||
NAT1 = r"CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)"
|
||||
NAT2 = r"SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')"
|
||||
NAT3 = r"CAST(SUBSTRING({}.{} FROM '(\d{{1,9}})$') AS integer)"
|
||||
|
||||
class NaturalOrderByManager(Manager):
|
||||
|
||||
class NaturalOrderingManager(Manager):
|
||||
"""
|
||||
Order objects naturally by a designated field. Leading and/or trailing digits of values within this field will be
|
||||
cast as independent integers and sorted accordingly. For example, "Foo2" will be ordered before "Foo10", even though
|
||||
the digit 1 is normally ordered before the digit 2.
|
||||
Order objects naturally by a designated field (defaults to 'name'). Leading and/or trailing digits of values within
|
||||
this field will be cast as independent integers and sorted accordingly. For example, "Foo2" will be ordered before
|
||||
"Foo10", even though the digit 1 is normally ordered before the digit 2.
|
||||
"""
|
||||
natural_order_field = None
|
||||
natural_order_field = 'name'
|
||||
|
||||
def get_queryset(self):
|
||||
|
||||
queryset = super(NaturalOrderByManager, self).get_queryset()
|
||||
queryset = super(NaturalOrderingManager, self).get_queryset()
|
||||
|
||||
db_table = self.model._meta.db_table
|
||||
db_field = self.natural_order_field
|
||||
|
||||
# Append the three subfields derived from the designated natural ordering field
|
||||
queryset = queryset.extra(select={
|
||||
'_nat1': r"CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)".format(db_table, db_field),
|
||||
'_nat2': r"SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')".format(db_table, db_field),
|
||||
'_nat3': r"CAST(SUBSTRING({}.{} FROM '(\d{{1,9}})$') AS integer)".format(db_table, db_field),
|
||||
'_nat1': NAT1.format(db_table, db_field),
|
||||
'_nat2': NAT2.format(db_table, db_field),
|
||||
'_nat3': NAT3.format(db_table, db_field),
|
||||
})
|
||||
|
||||
# Replace any instance of the designated natural ordering field with its three subfields
|
||||
|
Reference in New Issue
Block a user