1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move IPAddressManager to a separate file

This commit is contained in:
Jeremy Stretch
2020-01-14 12:07:45 -05:00
parent 6959785cd1
commit c084547dca
2 changed files with 17 additions and 15 deletions

16
netbox/ipam/managers.py Normal file
View File

@@ -0,0 +1,16 @@
from django.db import models
from django.db.models.expressions import RawSQL
class IPAddressManager(models.Manager):
def get_queryset(self):
"""
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. 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().get_queryset()
return qs.annotate(host=RawSQL('INET(HOST(ipam_ipaddress.address))', [])).order_by('family', 'host')