diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index 042057c7062..d1e721df0e1 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -265,10 +265,11 @@ class DeviceListView(CustomFieldModelAPIView, generics.ListAPIView): """ List devices (filterable) """ - queryset = Device.objects.select_related('device_type__manufacturer', 'device_role', 'tenant', 'platform', - 'rack__site', 'parent_bay').prefetch_related('primary_ip4__nat_outside', - 'primary_ip6__nat_outside', - 'custom_field_values__field') + queryset = Device.objects.select_related( + 'device_type__manufacturer', 'device_role', 'tenant', 'platform', 'site', 'rack', 'parent_bay' + ).prefetch_related( + 'primary_ip4__nat_outside', 'primary_ip6__nat_outside', 'custom_field_values__field' + ) serializer_class = serializers.DeviceSerializer filter_class = filters.DeviceFilter renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES + [BINDZoneRenderer, FlatJSONRenderer] @@ -278,8 +279,9 @@ class DeviceDetailView(CustomFieldModelAPIView, generics.RetrieveAPIView): """ Retrieve a single device """ - queryset = Device.objects.select_related('device_type__manufacturer', 'device_role', 'tenant', 'platform', - 'rack__site', 'parent_bay').prefetch_related('custom_field_values__field') + queryset = Device.objects.select_related( + 'device_type__manufacturer', 'device_role', 'tenant', 'platform', 'site', 'rack', 'parent_bay' + ).prefetch_related('custom_field_values__field') serializer_class = serializers.DeviceSerializer diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index e2c24c8c9ee..03d5b25874d 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -293,7 +293,7 @@ def count_racks(self): @property def count_devices(self): - return Device.objects.filter(rack__site=self).count() + return Device.objects.filter(site=self).count() @property def count_circuits(self): diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index 02d9acd1069..a66013dbd24 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -171,7 +171,7 @@ def site(request, slug): site = get_object_or_404(Site.objects.select_related('region', 'tenant__group'), slug=slug) stats = { 'rack_count': Rack.objects.filter(site=site).count(), - 'device_count': Device.objects.filter(rack__site=site).count(), + 'device_count': Device.objects.filter(site=site).count(), 'prefix_count': Prefix.objects.filter(site=site).count(), 'vlan_count': VLAN.objects.filter(site=site).count(), 'circuit_count': Circuit.objects.filter(terminations__site=site).count(), diff --git a/netbox/extras/management/commands/run_inventory.py b/netbox/extras/management/commands/run_inventory.py index dbbf8f3c9a7..ebfee92e053 100644 --- a/netbox/extras/management/commands/run_inventory.py +++ b/netbox/extras/management/commands/run_inventory.py @@ -49,7 +49,7 @@ def create_modules(modules, parent=None): self.stdout.write("Running inventory for these sites: {}".format(', '.join(site_names))) else: raise CommandError("One or more sites specified but none found.") - device_list = device_list.filter(rack__site__in=sites) + device_list = device_list.filter(site__in=sites) # --name: Filter devices by name matching a regex if options['name']: diff --git a/netbox/ipam/forms.py b/netbox/ipam/forms.py index 681a3c3a0c2..72b73208c5b 100644 --- a/netbox/ipam/forms.py +++ b/netbox/ipam/forms.py @@ -333,9 +333,11 @@ def __init__(self, *args, **kwargs): self.initial['nat_site'] = self.instance.nat_inside.interface.device.site.pk self.initial['nat_device'] = self.instance.nat_inside.interface.device.pk self.fields['nat_device'].queryset = Device.objects.filter( - rack__site=nat_inside.interface.device.site) + site=nat_inside.interface.device.site + ) self.fields['nat_inside'].queryset = IPAddress.objects.filter( - interface__device=nat_inside.interface.device) + interface__device=nat_inside.interface.device + ) else: self.fields['nat_inside'].queryset = IPAddress.objects.filter(pk=nat_inside.pk) @@ -343,9 +345,9 @@ def __init__(self, *args, **kwargs): # Initialize nat_device choices if nat_site is set if self.is_bound and self.data.get('nat_site'): - self.fields['nat_device'].queryset = Device.objects.filter(rack__site__pk=self.data['nat_site']) + self.fields['nat_device'].queryset = Device.objects.filter(site__pk=self.data['nat_site']) elif self.initial.get('nat_site'): - self.fields['nat_device'].queryset = Device.objects.filter(rack__site=self.initial['nat_site']) + self.fields['nat_device'].queryset = Device.objects.filter(site=self.initial['nat_site']) else: self.fields['nat_device'].choices = []