Coverage for ipam/managers.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.db.models import Manager 

2 

3from ipam.lookups import Host, Inet 

4from ipam.querysets import IPAddressQuerySet 

5 

6 

7class IPAddressManager(Manager.from_queryset(IPAddressQuerySet)): 

8 

9 def get_queryset(self): 

10 """ 

11 By default, PostgreSQL will order INETs with shorter (larger) prefix lengths ahead of those with longer 

12 (smaller) masks. This makes no sense when ordering IPs, which should be ordered solely by family and host 

13 address. We can use HOST() to extract just the host portion of the address (ignoring its mask), but we must 

14 then re-cast this value to INET() so that records will be ordered properly. We are essentially re-casting each 

15 IP address as a /32 or /128. 

16 

17 Host addresses are not unique, so we must also order by primary key to guarantee a stable, total ordering. 

18 Without this tiebreaker, PostgreSQL is free to return tied rows in a different order from one query to the 

19 next, which causes objects to be duplicated or omitted across paginated requests. 

20 """ 

21 return super().get_queryset().order_by(Inet(Host('address')), 'pk')