Arthur 1 ماه پیش
والد
کامیت
0c091aa80e
1فایلهای تغییر یافته به همراه24 افزوده شده و 25 حذف شده
  1. 24 25
      netbox/dcim/models/modules.py

+ 24 - 25
netbox/dcim/models/modules.py

@@ -5,6 +5,7 @@ from django.db import models
 from django.db.models.signals import post_save
 from django.utils.translation import gettext_lazy as _
 from jsonschema.exceptions import ValidationError as JSONValidationError
+from mptt.models import MPTTModel
 
 from dcim.choices import *
 from dcim.utils import update_interface_bridges
@@ -329,7 +330,13 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
                 component._location = self.device.location
                 component._rack = self.device.rack
 
-            if component_model is not ModuleBay:
+            if issubclass(component_model, MPTTModel):
+                # MPTT models must be saved individually to maintain tree structure
+                # Use delay_mptt_updates for better performance
+                with component_model.objects.delay_mptt_updates():
+                    for instance in create_instances:
+                        instance.save()
+            else:
                 component_model.objects.bulk_create(create_instances)
                 # Emit the post_save signal for each newly created object
                 for component in create_instances:
@@ -341,16 +348,25 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
                         using='default',
                         update_fields=None
                     )
-            else:
-                # ModuleBays must be saved individually for MPTT
-                # Use delay_mptt_updates for better performance when creating multiple ModuleBays
-                with ModuleBay.objects.delay_mptt_updates():
-                    for instance in create_instances:
-                        instance.save()
 
             update_fields = ['module']
 
-            if component_model is not ModuleBay:
+            if issubclass(component_model, MPTTModel):
+                # MPTT models must be saved individually to maintain tree structure
+                # Use delay_mptt_updates for better performance - could do bulk_update
+                # but then would need to rebuild the tree after the updates.
+                with component_model.objects.delay_mptt_updates():
+                    for component in update_instances:
+                        component.save()
+                        post_save.send(
+                            sender=component_model,
+                            instance=component,
+                            created=False,
+                            raw=False,
+                            using='default',
+                            update_fields=update_fields
+                        )
+            else:
                 component_model.objects.bulk_update(update_instances, update_fields)
                 # Emit the post_save signal for each updated object
                 for component in update_instances:
@@ -362,23 +378,6 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
                         using='default',
                         update_fields=update_fields
                     )
-            else:
-                # ModuleBays must be saved individually to maintain MPTT tree structure
-                # Use delay_mptt_updates for better performance
-                with ModuleBay.objects.delay_mptt_updates():
-                    for component in update_instances:
-                        component.save()
-                        post_save.send(
-                            sender=component_model,
-                            instance=component,
-                            created=False,
-                            raw=False,
-                            using='default',
-                            update_fields=update_fields
-                        )
-                # Rebuild the tree once to apply order_insertion_by after all operations
-                if create_instances or update_instances:
-                    ModuleBay.objects.rebuild()
 
         # Interface bridges have to be set after interface instantiation
         update_interface_bridges(self.device, self.module_type.interfacetemplates, self)