From d6e7e59c96c34cf2666536895ded3356927525c3 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Wed, 9 Apr 2025 09:23:28 -0400 Subject: [PATCH] ordering for child and parent prefixes --- netbox/ipam/graphql/types.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/netbox/ipam/graphql/types.py b/netbox/ipam/graphql/types.py index d92191991..e77ba2df1 100644 --- a/netbox/ipam/graphql/types.py +++ b/netbox/ipam/graphql/types.py @@ -142,13 +142,14 @@ class IPAddressType(NetBoxObjectType, ContactsMixin, BaseIPAddressFamilyType): @strawberry_django.field def parent_prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]: """ - Return all prefixes containing this IP address. + Return all prefixes containing this IP address, sorted by depth in descending order. + The closest containing parent prefix will be first. """ from ipam.models import Prefix return Prefix.objects.filter( vrf=self.vrf, prefix__net_contains_or_equals=str(self.address.ip) - ) + ).order_by('-_depth') @strawberry_django.type( @@ -188,24 +189,27 @@ class PrefixType(NetBoxObjectType, ContactsMixin, BaseIPAddressFamilyType): @strawberry_django.field def parent_prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]: """ - Return all parent prefixes containing this prefix. + Return all parent prefixes containing this prefix, sorted by depth in descending order. + The closest containing parent prefix will be first. """ from ipam.models import Prefix return Prefix.objects.filter( vrf=self.vrf, prefix__net_contains=str(self.prefix) - ) + ).order_by('-_depth') @strawberry_django.field def child_prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]: """ - Return all child prefixes contained within this prefix. + Return all child prefixes contained within this prefix, sorted by depth and then by network. """ from ipam.models import Prefix + + # Get child prefixes sorted by depth first and then by prefix naturally return Prefix.objects.filter( vrf=self.vrf, prefix__net_contained=str(self.prefix) - ) + ).order_by('_depth', 'prefix') @strawberry_django.field def child_ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]: