mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Added child counts to API serializers (WIP)
This commit is contained in:
@@ -15,10 +15,11 @@ __all__ = [
|
||||
|
||||
class NestedTenantGroupSerializer(WritableNestedSerializer):
|
||||
url = serializers.HyperlinkedIdentityField(view_name='tenancy-api:tenantgroup-detail')
|
||||
tenant_count = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = TenantGroup
|
||||
fields = ['id', 'url', 'name', 'slug']
|
||||
fields = ['id', 'url', 'name', 'slug', 'tenant_count']
|
||||
|
||||
|
||||
class NestedTenantSerializer(WritableNestedSerializer):
|
||||
|
@@ -1,3 +1,4 @@
|
||||
from rest_framework import serializers
|
||||
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
|
||||
|
||||
from extras.api.customfields import CustomFieldModelSerializer
|
||||
@@ -11,10 +12,11 @@ from .nested_serializers import *
|
||||
#
|
||||
|
||||
class TenantGroupSerializer(ValidatedModelSerializer):
|
||||
tenant_count = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = TenantGroup
|
||||
fields = ['id', 'name', 'slug']
|
||||
fields = ['id', 'name', 'slug', 'tenant_count']
|
||||
|
||||
|
||||
class TenantSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
||||
|
@@ -1,3 +1,5 @@
|
||||
from django.db.models import Count
|
||||
|
||||
from extras.api.views import CustomFieldModelViewSet
|
||||
from tenancy import filters
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
@@ -18,7 +20,9 @@ class TenancyFieldChoicesViewSet(FieldChoicesViewSet):
|
||||
#
|
||||
|
||||
class TenantGroupViewSet(ModelViewSet):
|
||||
queryset = TenantGroup.objects.all()
|
||||
queryset = TenantGroup.objects.annotate(
|
||||
tenant_count=Count('tenants')
|
||||
)
|
||||
serializer_class = serializers.TenantGroupSerializer
|
||||
filterset_class = filters.TenantGroupFilter
|
||||
|
||||
|
@@ -36,7 +36,7 @@ class TenantGroupTest(APITestCase):
|
||||
|
||||
self.assertEqual(
|
||||
sorted(response.data['results'][0]),
|
||||
['id', 'name', 'slug', 'url']
|
||||
['id', 'name', 'slug', 'tenant_count', 'url']
|
||||
)
|
||||
|
||||
def test_create_tenantgroup(self):
|
||||
|
Reference in New Issue
Block a user