managers.py 815 B

1234567891011121314151617
  1. from django.db.models import Manager
  2. from ipam.lookups import Host, Inet
  3. from utilities.querysets import RestrictedQuerySet
  4. class IPAddressManager(Manager.from_queryset(RestrictedQuerySet)):
  5. def get_queryset(self):
  6. """
  7. By default, PostgreSQL will order INETs with shorter (larger) prefix lengths ahead of those with longer
  8. (smaller) masks. This makes no sense when ordering IPs, which should be ordered solely by family and host
  9. address. We can use HOST() to extract just the host portion of the address (ignoring its mask), but we must
  10. then re-cast this value to INET() so that records will be ordered properly. We are essentially re-casting each
  11. IP address as a /32 or /128.
  12. """
  13. return super().get_queryset().order_by(Inet(Host('address')))