Просмотр исходного кода

implemented #2350 - config context included by default in API

John Anderson 7 лет назад
Родитель
Сommit
bd573fd5cf
2 измененных файлов с 25 добавлено и 4 удалено
  1. 14 0
      CHANGELOG.md
  2. 11 4
      netbox/dcim/api/views.py

+ 14 - 0
CHANGELOG.md

@@ -1,3 +1,17 @@
+v2.6.0 (FUTURE)
+
+## Changes
+
+### API Device/VM Config Context Included by Default ([#2350](https://github.com/digitalocean/netbox/issues/2350))
+
+The rendered Config Context for Devices and VMs is now included by default in all API results (list and detail views).
+Previously the rendered Config Context was only available in the detail view for objects. Users with large amounts of
+context data may observe a performance drop when returning multiple objects. To combat this, in cases where the rendered
+Config Context is not needed, the query parameter `?exclude=config_context` may be added to the request as to remove
+the Config Context from being included in any results.
+
+---
+
 v2.5.7 (FUTURE)
 
 ## Enhancements

+ 11 - 4
netbox/dcim/api/views.py

@@ -291,16 +291,23 @@ class DeviceViewSet(CustomFieldModelViewSet):
 
     def get_serializer_class(self):
         """
-        Include rendered config context when retrieving a single Device.
+        Select the specific serializer based on the request context.
+
+        If the `brief` query param equates to True, return the NestedDeviceSerializer
+
+        If the `exclude` query param includes `config_context` as a value, return the DeviceSerializer
+
+        Else, return the DeviceWithConfigContextSerializer
         """
-        if self.action == 'retrieve':
-            return serializers.DeviceWithConfigContextSerializer
 
         request = self.get_serializer_context()['request']
         if request.query_params.get('brief', False):
             return serializers.NestedDeviceSerializer
 
-        return serializers.DeviceSerializer
+        elif 'config_context' in request.query_params.get('exclude', []):
+            return serializers.DeviceSerializer
+
+        return serializers.DeviceWithConfigContextSerializer
 
     @action(detail=True, url_path='napalm')
     def napalm(self, request, pk):