Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of SQL queries made in API calls #1535

Closed
wants to merge 3 commits into from
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
13 changes: 11 additions & 2 deletions netbox/dcim/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class RackRoleViewSet(ModelViewSet):
#

class RackViewSet(WritableSerializerMixin, CustomFieldModelViewSet):
queryset = Rack.objects.select_related('site', 'group__site', 'tenant')
queryset = Rack.objects.select_related('site', 'group__site', 'tenant', 'role')
serializer_class = serializers.RackSerializer
write_serializer_class = serializers.WritableRackSerializer
filter_class = filters.RackFilter
Expand Down Expand Up @@ -219,6 +219,7 @@ class DeviceViewSet(WritableSerializerMixin, CustomFieldModelViewSet):
queryset = Device.objects.select_related(
'device_type__manufacturer', 'device_role', 'tenant', 'platform', 'site', 'rack', 'parent_bay',
).prefetch_related(
'primary_ip4', 'primary_ip6',
'primary_ip4__nat_outside', 'primary_ip6__nat_outside',
)
serializer_class = serializers.DeviceSerializer
Expand Down Expand Up @@ -320,7 +321,15 @@ class PowerOutletViewSet(WritableSerializerMixin, ModelViewSet):


class InterfaceViewSet(WritableSerializerMixin, ModelViewSet):
queryset = Interface.objects.select_related('device')
queryset = Interface.objects.select_related(
'circuit_termination__circuit',
'circuit_termination__interface',
'connected_as_a__interface_b',
'connected_as_a__interface_b__device',
'connected_as_b__interface_a',
'connected_as_b__interface_a__device',
'device',
'lag')
serializer_class = serializers.InterfaceSerializer
write_serializer_class = serializers.WritableInterfaceSerializer
filter_class = filters.InterfaceFilter
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ class RecentActivityViewSet(ReadOnlyModelViewSet):
"""
List all UserActions to provide a log of recent activity.
"""
queryset = UserAction.objects.all()
queryset = UserAction.objects.select_related('user')
serializer_class = serializers.UserActionSerializer
filter_class = filters.UserActionFilter
13 changes: 12 additions & 1 deletion netbox/ipam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,18 @@ def available_ips(self, request, pk=None):
#

class IPAddressViewSet(WritableSerializerMixin, CustomFieldModelViewSet):
queryset = IPAddress.objects.select_related('vrf__tenant', 'tenant', 'interface__device', 'nat_inside')
queryset = IPAddress.objects.select_related(
'interface',
'interface__circuit_termination',
'interface__connected_as_a__interface_b',
'interface__connected_as_a__interface_b__device',
'interface__connected_as_b__interface_a',
'interface__connected_as_b__interface_a__device',
'interface__device',
'nat_inside',
'nat_outside',
'tenant',
'vrf__tenant')
serializer_class = serializers.IPAddressSerializer
write_serializer_class = serializers.WritableIPAddressSerializer
filter_class = filters.IPAddressFilter
Expand Down