@@ -22,9 +22,10 @@ def get_for_object(self, obj, aggregate_data=False):
22
22
aggregate_data: If True, use the JSONBAgg aggregate function to return only the list of JSON data objects
23
23
"""
24
24
25
- # Device type and location assignment is relevant only for Devices
25
+ # Device type and location assignment are relevant only for Devices
26
26
device_type = getattr (obj , 'device_type' , None )
27
27
location = getattr (obj , 'location' , None )
28
+ locations = location .get_ancestors (include_self = True ) if location else []
28
29
29
30
# Get assigned cluster, group, and type (if any)
30
31
cluster = getattr (obj , 'cluster' , None )
@@ -49,7 +50,7 @@ def get_for_object(self, obj, aggregate_data=False):
49
50
Q (regions__in = regions ) | Q (regions = None ),
50
51
Q (site_groups__in = sitegroups ) | Q (site_groups = None ),
51
52
Q (sites = obj .site ) | Q (sites = None ),
52
- Q (locations = location ) | Q (locations = None ),
53
+ Q (locations__in = locations ) | Q (locations = None ),
53
54
Q (device_types = device_type ) | Q (device_types = None ),
54
55
Q (roles__in = device_roles ) | Q (roles = None ),
55
56
Q (platforms = obj .platform ) | Q (platforms = None ),
@@ -124,7 +125,15 @@ def _get_config_context_filters(self):
124
125
125
126
# Apply Location & DeviceType filters only for VirtualMachines
126
127
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
+ )
128
137
base_query .add ((Q (device_types = OuterRef ('device_type' )) | Q (device_types = None )), Q .AND )
129
138
elif self .model ._meta .model_name == 'virtualmachine' :
130
139
base_query .add (Q (locations = None ), Q .AND )
0 commit comments