Andrey Tikhonov пре 1 година
родитељ
комит
9c3e7f2c5d
2 измењених фајлова са 5 додато и 54 уклоњено
  1. 5 9
      netbox/dcim/api/views.py
  2. 0 45
      netbox/dcim/models/cables.py

+ 5 - 9
netbox/dcim/api/views.py

@@ -434,21 +434,17 @@ class PowerOutletViewSet(PathEndpointMixin, NetBoxModelViewSet):
 
 class InterfaceViewSet(PathEndpointMixin, NetBoxModelViewSet):
     queryset = Interface.objects.prefetch_related(
-        # '_path',
-        # 'cable__terminations',
         GenericPrefetch(
             "cable__terminations__termination",
             [
                 Interface.objects.select_related("device", "cable"),
             ],
         ),
-        Prefetch(
-            "_path",
-            CablePath.objects.prefetch_related(
-                GenericPrefetch("path_objects", [
-                    Interface.objects.select_related("device"),
-                ]),
-            )
+        GenericPrefetch(
+            "_path__path_objects",
+            [
+                Interface.objects.select_related("device", "cable"),
+            ],
         ),
         'l2vpn_terminations',  # Referenced by InterfaceSerializer.l2vpn_termination
         'ip_addresses',  # Referenced by Interface.count_ipaddresses()

+ 0 - 45
netbox/dcim/models/cables.py

@@ -490,15 +490,6 @@ class CablePath(models.Model):
             ct_id, _ = decompile_path_node(self.path[-1][0])
             return ObjectType.objects.get_for_id(ct_id)
 
-    @property
-    def path_objects_old(self):
-        """
-        Cache and return the complete path as lists of objects, derived from their annotation within the path.
-        """
-        if not hasattr(self, '_path_objects'):
-            self._path_objects = self._get_path()
-        return self._path_objects
-
     @property
     def _path_decompiled(self):
         res = []
@@ -747,42 +738,6 @@ class CablePath(models.Model):
             self.delete()
     retrace.alters_data = True
 
-    def _get_path(self):
-        """
-        Return the path as a list of prefetched objects.
-        """
-        # Compile a list of IDs to prefetch for each type of model in the path
-        to_prefetch = defaultdict(list)
-        for node in self._nodes:
-            ct_id, object_id = decompile_path_node(node)
-            to_prefetch[ct_id].append(object_id)
-
-        # Prefetch path objects using one query per model type. Prefetch related devices where appropriate.
-        prefetched = {}
-        for ct_id, object_ids in to_prefetch.items():
-            model_class = ObjectType.objects.get_for_id(ct_id).model_class()
-            queryset = model_class.objects.filter(pk__in=object_ids)
-            if hasattr(model_class, 'device'):
-                queryset = queryset.prefetch_related('device')
-            prefetched[ct_id] = {
-                obj.id: obj for obj in queryset
-            }
-
-        # Replicate the path using the prefetched objects.
-        path = []
-        for step in self.path:
-            nodes = []
-            for node in step:
-                ct_id, object_id = decompile_path_node(node)
-                try:
-                    nodes.append(prefetched[ct_id][object_id])
-                except KeyError:
-                    # Ignore stale (deleted) object IDs
-                    pass
-            path.append(nodes)
-
-        return path
-
     def get_cable_ids(self):
         """
         Return all Cable IDs within the path.