Skip to content

Commit bf73564

Browse files
authored
fix(extras): Inherit ConfigContext from ancestors locations (#20291)
1 parent 0e627d4 commit bf73564

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

netbox/extras/querysets.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ def get_for_object(self, obj, aggregate_data=False):
2222
aggregate_data: If True, use the JSONBAgg aggregate function to return only the list of JSON data objects
2323
"""
2424

25-
# Device type and location assignment is relevant only for Devices
25+
# Device type and location assignment are relevant only for Devices
2626
device_type = getattr(obj, 'device_type', None)
2727
location = getattr(obj, 'location', None)
28+
locations = location.get_ancestors(include_self=True) if location else []
2829

2930
# Get assigned cluster, group, and type (if any)
3031
cluster = getattr(obj, 'cluster', None)
@@ -49,7 +50,7 @@ def get_for_object(self, obj, aggregate_data=False):
4950
Q(regions__in=regions) | Q(regions=None),
5051
Q(site_groups__in=sitegroups) | Q(site_groups=None),
5152
Q(sites=obj.site) | Q(sites=None),
52-
Q(locations=location) | Q(locations=None),
53+
Q(locations__in=locations) | Q(locations=None),
5354
Q(device_types=device_type) | Q(device_types=None),
5455
Q(roles__in=device_roles) | Q(roles=None),
5556
Q(platforms=obj.platform) | Q(platforms=None),
@@ -124,7 +125,15 @@ def _get_config_context_filters(self):
124125

125126
# Apply Location & DeviceType filters only for VirtualMachines
126127
if self.model._meta.model_name == 'device':
127-
base_query.add((Q(locations=OuterRef('location')) | Q(locations=None)), Q.AND)
128+
base_query.add(
129+
(Q(
130+
locations__tree_id=OuterRef('location__tree_id'),
131+
locations__level__lte=OuterRef('location__level'),
132+
locations__lft__lte=OuterRef('location__lft'),
133+
locations__rght__gte=OuterRef('location__rght'),
134+
) | Q(locations=None)),
135+
Q.AND
136+
)
128137
base_query.add((Q(device_types=OuterRef('device_type')) | Q(device_types=None)), Q.AND)
129138
elif self.model._meta.model_name == 'virtualmachine':
130139
base_query.add(Q(locations=None), Q.AND)

0 commit comments

Comments
 (0)