From b0c70a586fd2838aaf9ed15f0cce5dacec1568cc Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 15 Aug 2016 15:39:48 -0400 Subject: [PATCH] Fixes #460: For real this time --- netbox/ipam/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/netbox/ipam/models.py b/netbox/ipam/models.py index 65724d410..eebc43fad 100644 --- a/netbox/ipam/models.py +++ b/netbox/ipam/models.py @@ -302,10 +302,12 @@ class IPAddressManager(models.Manager): """ By default, PostgreSQL will order INETs with shorter (larger) prefix lengths ahead of those with longer (smaller) masks. This makes no sense when ordering IPs, which should be ordered solely by family and host - address. Here, we alter the default ordering to use HOST(address) instead of the raw address value. + address. We can use HOST() to extract just the host portion of the address (ignoring its mask), but we must + then re-cast this value to INET() so that records will be ordered properly. We are essentially re-casting each + IP address as a /32 or /128. """ qs = super(IPAddressManager, self).get_queryset() - return qs.annotate(host=RawSQL('HOST(ipam_ipaddress.address)', [])).order_by('family', 'host') + return qs.annotate(host=RawSQL('INET(HOST(ipam_ipaddress.address))', [])).order_by('family', 'host') class IPAddress(CreatedUpdatedModel):