From 23fddf74b6e07a8d15985f380dff23f8563a6a16 Mon Sep 17 00:00:00 2001 From: Jason Yates Date: Sun, 10 Apr 2022 09:06:14 +0100 Subject: [PATCH] Updating to use a single queryset Updating to use a single queryset for both template variables --- netbox/dcim/views.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index 9b24ea9d1..bc2cbdcb8 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -342,17 +342,12 @@ class SiteView(generic.ObjectView): 'device_count', cumulative=True ).restrict(request.user, 'view').filter(site=instance) + nonracked_devices = Device.objects.filter( site=instance, position__isnull=True, parent_bay__isnull=True - ).prefetch_related('device_type__manufacturer').order_by('-pk')[:10] - - total_nonracked_devices_count = Device.objects.filter( - site=instance, - position__isnull=True, - parent_bay__isnull=True - ).count() + ).prefetch_related('device_type__manufacturer') asns = ASN.objects.restrict(request.user, 'view').filter(sites=instance) asn_count = asns.count() @@ -363,8 +358,8 @@ class SiteView(generic.ObjectView): 'stats': stats, 'locations': locations, 'asns': asns, - 'nonracked_devices': nonracked_devices, - 'total_nonracked_devices_count': total_nonracked_devices_count, + 'nonracked_devices': nonracked_devices.order_by('-pk')[:10], + 'total_nonracked_devices_count': nonracked_devices.count(), } @@ -442,24 +437,19 @@ class LocationView(generic.ObjectView): ).filter(pk__in=location_ids).exclude(pk=instance.pk) child_locations_table = tables.LocationTable(child_locations) child_locations_table.configure(request) + nonracked_devices = Device.objects.filter( location=instance, position__isnull=True, parent_bay__isnull=True - ).prefetch_related('device_type__manufacturer').order_by('-pk')[:10] - - total_nonracked_devices_count = Device.objects.filter( - location=instance, - position__isnull=True, - parent_bay__isnull=True - ).count() + ).prefetch_related('device_type__manufacturer') return { 'rack_count': rack_count, 'device_count': device_count, 'child_locations_table': child_locations_table, - 'nonracked_devices': nonracked_devices, - 'total_nonracked_devices_count': total_nonracked_devices_count, + 'nonracked_devices': nonracked_devices.order_by('-pk')[:10], + 'total_nonracked_devices_count': nonracked_devices.count(), }