managers.py 755 B

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