Repository: netbox-community/netbox

Issue:
Add cable stat for tenant
### NetBox version

v3.1.4

### Feature type

Change to existing functionality

### Proposed functionality

I would like to have a stats field for cables on the tenant screen. Same as for VLAN, circuits, etc.

![image](https://user-images.githubusercontent.com/36925394/148442353-76da2106-f617-4f0a-95a2-7745be4d0355.png)


### Use case

 It will make a better overview of the services provided to the tenant.

### Database changes

I believe no change is required in the database.

### External dependencies

_No response_


Discussion:

Resolving patch:
diff --git a/netbox/tenancy/api/serializers.py b/netbox/tenancy/api/serializers.py
--- a/netbox/tenancy/api/serializers.py
+++ b/netbox/tenancy/api/serializers.py
@@ -40,13 +40,14 @@ class TenantSerializer(PrimaryModelSerializer):
     vlan_count = serializers.IntegerField(read_only=True)
     vrf_count = serializers.IntegerField(read_only=True)
     cluster_count = serializers.IntegerField(read_only=True)
+    cable_count = serializers.IntegerField(read_only=True)
 
     class Meta:
         model = Tenant
         fields = [
             'id', 'url', 'display', 'name', 'slug', 'group', 'description', 'comments', 'tags', 'custom_fields',
             'created', 'last_updated', 'circuit_count', 'device_count', 'ipaddress_count', 'prefix_count', 'rack_count',
-            'site_count', 'virtualmachine_count', 'vlan_count', 'vrf_count', 'cluster_count',
+            'site_count', 'virtualmachine_count', 'vlan_count', 'vrf_count', 'cluster_count', 'cable_count',
         ]
 
 
diff --git a/netbox/tenancy/api/views.py b/netbox/tenancy/api/views.py
--- a/netbox/tenancy/api/views.py
+++ b/netbox/tenancy/api/views.py
@@ -1,7 +1,7 @@
 from rest_framework.routers import APIRootView
 
 from circuits.models import Circuit
-from dcim.models import Device, Rack, Site
+from dcim.models import Device, Rack, Site, Cable
 from extras.api.views import CustomFieldModelViewSet
 from ipam.models import IPAddress, Prefix, VLAN, VRF
 from tenancy import filtersets
@@ -47,7 +47,8 @@ class TenantViewSet(CustomFieldModelViewSet):
         site_count=count_related(Site, 'tenant'),
         virtualmachine_count=count_related(VirtualMachine, 'tenant'),
         vlan_count=count_related(VLAN, 'tenant'),
-        vrf_count=count_related(VRF, 'tenant')
+        vrf_count=count_related(VRF, 'tenant'),
+        cable_count=count_related(Cable, 'tenant')
     )
     serializer_class = serializers.TenantSerializer
     filterset_class = filtersets.TenantFilterSet
diff --git a/netbox/tenancy/views.py b/netbox/tenancy/views.py
--- a/netbox/tenancy/views.py
+++ b/netbox/tenancy/views.py
@@ -3,7 +3,7 @@
 from django.shortcuts import get_object_or_404
 
 from circuits.models import Circuit
-from dcim.models import Site, Rack, Device, RackReservation
+from dcim.models import Site, Rack, Device, RackReservation, Cable
 from ipam.models import Aggregate, IPAddress, Prefix, VLAN, VRF
 from netbox.views import generic
 from utilities.tables import paginate_table
@@ -112,6 +112,7 @@ def get_extra_context(self, request, instance):
             'circuit_count': Circuit.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
             'virtualmachine_count': VirtualMachine.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
             'cluster_count': Cluster.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
+            'cable_count': Cable.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
         }
 
         return {


Test patch: