diff --git a/netbox/dcim/api/serializers.py b/netbox/dcim/api/serializers.py index 698e7d2d6f1..f81f299af1e 100644 --- a/netbox/dcim/api/serializers.py +++ b/netbox/dcim/api/serializers.py @@ -134,12 +134,13 @@ class Meta(ManufacturerSerializer.Meta): class DeviceTypeSerializer(CustomFieldSerializer, serializers.ModelSerializer): manufacturer = ManufacturerNestedSerializer() subdevice_role = serializers.SerializerMethodField() + instance_count = serializers.IntegerField(source='instances.count', read_only=True) class Meta: model = DeviceType fields = ['id', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth', 'interface_ordering', 'is_console_server', 'is_pdu', 'is_network_device', 'subdevice_role', - 'comments', 'custom_fields'] + 'comments', 'custom_fields', 'instance_count'] def get_subdevice_role(self, obj): return { diff --git a/netbox/dcim/api/views.py b/netbox/dcim/api/views.py index 13e69392fd6..70ca17bbc0a 100644 --- a/netbox/dcim/api/views.py +++ b/netbox/dcim/api/views.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib.contenttypes.models import ContentType +from django.db.models import Count from django.http import Http404 from django.shortcuts import get_object_or_404 diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index 9931ffd8b21..95fb8389251 100644 --- a/netbox/dcim/forms.py +++ b/netbox/dcim/forms.py @@ -639,18 +639,45 @@ class Meta: class DeviceFilterForm(BootstrapMixin, CustomFieldFilterForm): model = Device - site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('racks__devices')), to_field_name='slug') - rack_group_id = FilterChoiceField(queryset=RackGroup.objects.annotate(filter_count=Count('racks__devices')), - label='Rack Group') - role = FilterChoiceField(queryset=DeviceRole.objects.annotate(filter_count=Count('devices')), to_field_name='slug') - tenant = FilterChoiceField(queryset=Tenant.objects.annotate(filter_count=Count('devices')), to_field_name='slug', - null_option=(0, 'None')) - device_type_id = FilterChoiceField(queryset=DeviceType.objects.select_related('manufacturer') - .annotate(filter_count=Count('instances')), label='Type') - platform = FilterChoiceField(queryset=Platform.objects.annotate(filter_count=Count('devices')), - to_field_name='slug', null_option=(0, 'None')) - status = forms.NullBooleanField(required=False, widget=forms.Select(choices=FORM_STATUS_CHOICES)) - mac_address = forms.CharField(required=False, label='MAC address') + site = FilterChoiceField( + queryset=Site.objects.annotate(filter_count=Count('racks__devices')), + to_field_name='slug', + ) + rack_group_id = FilterChoiceField( + queryset=RackGroup.objects.select_related('site').annotate(filter_count=Count('racks__devices')), + label='Rack Group', + ) + role = FilterChoiceField( + queryset=DeviceRole.objects.annotate(filter_count=Count('devices')), + to_field_name='slug', + ) + tenant = FilterChoiceField( + queryset=Tenant.objects.annotate(filter_count=Count('devices')), to_field_name='slug', + null_option=(0, 'None'), + ) + manufacturer_id = FilterChoiceField( + queryset=Manufacturer.objects.all(), + label='Manufacturer', + ) + device_type_id = FilterChoiceField( + queryset=DeviceType.objects.select_related('manufacturer').order_by('model').annotate( + filter_count=Count('instances'), + ), + label='Model', + ) + platform = FilterChoiceField( + queryset=Platform.objects.annotate(filter_count=Count('devices')), + to_field_name='slug', + null_option=(0, 'None'), + ) + status = forms.NullBooleanField( + required=False, + widget=forms.Select(choices=FORM_STATUS_CHOICES), + ) + mac_address = forms.CharField( + required=False, + label='MAC address', + ) # diff --git a/netbox/templates/dcim/device_list.html b/netbox/templates/dcim/device_list.html index 18bc7d62755..b543154ec03 100644 --- a/netbox/templates/dcim/device_list.html +++ b/netbox/templates/dcim/device_list.html @@ -28,3 +28,28 @@