1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #1931: Added a count of assigned IP addresses to the interface API serializer

This commit is contained in:
Jeremy Stretch
2018-11-05 14:32:22 -05:00
parent e8caa46484
commit 46e594f1f1
3 changed files with 7 additions and 3 deletions

View File

@ -712,7 +712,7 @@ class InterfaceSerializer(TaggitSerializer, ValidatedModelSerializer):
model = Interface model = Interface
fields = [ fields = [
'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description', 'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description',
'connected_endpoint', 'cable', 'mode', 'untagged_vlan', 'tagged_vlans', 'tags', 'connected_endpoint', 'cable', 'mode', 'untagged_vlan', 'tagged_vlans', 'tags', 'count_ipaddresses',
] ]
def validate(self, data): def validate(self, data):

View File

@ -1,7 +1,7 @@
from collections import OrderedDict from collections import OrderedDict
from django.conf import settings from django.conf import settings
from django.db.models import F, Q from django.db.models import Count, F, Q
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from drf_yasg import openapi from drf_yasg import openapi
@ -409,7 +409,7 @@ class InterfaceViewSet(CableTraceMixin, ModelViewSet):
queryset = Interface.objects.select_related( queryset = Interface.objects.select_related(
'device', '_connected_interface', '_connected_circuittermination', 'cable' 'device', '_connected_interface', '_connected_circuittermination', 'cable'
).prefetch_related( ).prefetch_related(
'tags' 'ip_addresses', 'tags'
) )
serializer_class = serializers.InterfaceSerializer serializer_class = serializers.InterfaceSerializer
filterset_class = filters.InterfaceFilter filterset_class = filters.InterfaceFilter

View File

@ -2091,6 +2091,10 @@ class Interface(CableTermination, ComponentModel):
def is_lag(self): def is_lag(self):
return self.form_factor == IFACE_FF_LAG return self.form_factor == IFACE_FF_LAG
@property
def count_ipaddresses(self):
return self.ip_addresses.count()
# #
# Pass-through ports # Pass-through ports