Переглянути джерело

Prefetch installed components when adding modules

kkthxbye-code 3 роки тому
батько
коміт
c52aa2196d
1 змінених файлів з 6 додано та 2 видалено
  1. 6 2
      netbox/dcim/models/devices.py

+ 6 - 2
netbox/dcim/models/devices.py

@@ -1086,13 +1086,17 @@ class Module(NetBoxModel, ConfigContextModel):
             create_instances = []
             update_instances = []
 
+            # Prefetch installed components
+            installed_components = {
+                component.name: component for component in getattr(self.device, component_attribute).filter(module__isnull=True)
+            }
+
             # Get the template for the module type.
             for template in getattr(self.module_type, templates).all():
                 template_instance = template.instantiate(device=self.device, module=self)
 
                 if adopt_components:
-                    existing_item = getattr(self.device, component_attribute).filter(
-                        module__isnull=True, name=template_instance.name).first()
+                    existing_item = installed_components.get(template_instance.name)
 
                     # Check if there's a component with the same name already
                     if existing_item: