Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion netbox/ipam/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.contenttypes.models import ContentType
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers

Expand All @@ -24,7 +25,7 @@ class ASNRangeSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:asnrange-detail')
rir = NestedRIRSerializer()
tenant = NestedTenantSerializer(required=False, allow_null=True)
asn_count = serializers.IntegerField(read_only=True)
asn_count = serializers.SerializerMethodField(read_only=True)

class Meta:
model = ASNRange
Expand All @@ -33,6 +34,10 @@ class Meta:
'custom_fields', 'created', 'last_updated', 'asn_count',
]

@extend_schema_field(OpenApiTypes.INT)
def get_asn_count(self, obj):
return obj.get_child_asns().count()

Comment on lines +37 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two issues with this approach:

  1. It generates a separate query per object in the table.
  2. It prevents us from ordering the table by ASN count.

I've opened #13053 with a more complete (albeit more complex) solution. Please let me know what you think.


#
# ASNs
Expand Down
8 changes: 4 additions & 4 deletions netbox/ipam/tables/asn.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class ASNRangeTable(TenancyColumnsMixin, NetBoxTable):
tags = columns.TagColumn(
url_name='ipam:asnrange_list'
)
asn_count = columns.LinkedCountColumn(
viewname='ipam:asn_list',
url_params={'asn_id': 'pk'},
verbose_name=_('ASN Count')
asn_count = columns.TemplateColumn(
orderable=False,
verbose_name=_('ASN Count'),
template_code='''{{ record.get_child_asns.count }}'''
)

class Meta(NetBoxTable.Meta):
Expand Down