2020-01-14 12:07:45 -05:00
|
|
|
from django.db import models
|
2020-02-19 17:17:41 -05:00
|
|
|
|
|
|
|
from ipam.lookups import Host, Inet
|
2020-05-29 16:27:36 -04:00
|
|
|
from utilities.querysets import RestrictedQuerySet
|
2020-01-14 12:07:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2020-05-29 16:27:36 -04:00
|
|
|
qs = RestrictedQuerySet(self.model, using=self._db)
|
2020-02-21 15:26:55 -05:00
|
|
|
return qs.order_by(Inet(Host('address')))
|