rebuild tree after rename
Some checks are pending
CI / build (20.x, 3.12) (push) Waiting to run
CI / build (20.x, 3.13) (push) Waiting to run
CI / build (20.x, 3.14) (push) Waiting to run

This commit is contained in:
Arthur
2026-01-20 15:28:49 -08:00
parent c4c3518bb4
commit be1a008216
3 changed files with 47 additions and 17 deletions

View File

@@ -1273,7 +1273,7 @@ class ModuleBay(ModularComponentModel, TrackingModelMixin, MPTTModel):
verbose_name_plural = _('module bays') verbose_name_plural = _('module bays')
class MPTTMeta: class MPTTMeta:
order_insertion_by = ('name',) order_insertion_by = ('module', 'name',)
def clean(self): def clean(self):
super().clean() super().clean()

View File

@@ -343,10 +343,14 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
) )
else: else:
# ModuleBays must be saved individually for MPTT # 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: for instance in create_instances:
instance.save() instance.save()
update_fields = ['module'] update_fields = ['module']
if component_model is not ModuleBay:
component_model.objects.bulk_update(update_instances, update_fields) component_model.objects.bulk_update(update_instances, update_fields)
# Emit the post_save signal for each updated object # Emit the post_save signal for each updated object
for component in update_instances: for component in update_instances:
@@ -358,6 +362,23 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
using='default', using='default',
update_fields=update_fields 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 # Interface bridges have to be set after interface instantiation
update_interface_bridges(self.device, self.module_type.interfacetemplates, self) update_interface_bridges(self.device, self.module_type.interfacetemplates, self)

View File

@@ -895,6 +895,15 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView):
renamed_pks = self._rename_objects(form, selected_objects) renamed_pks = self._rename_objects(form, selected_objects)
if '_apply' in request.POST: if '_apply' in request.POST:
# For MPTT models, delay tree updates until all saves are complete
if issubclass(self.queryset.model, MPTTModel):
with self.queryset.model.objects.delay_mptt_updates():
for obj in selected_objects:
setattr(obj, self.field_name, obj.new_name)
obj.save()
# Rebuild the tree to apply order_insertion_by after renaming
self.queryset.model.objects.rebuild()
else:
for obj in selected_objects: for obj in selected_objects:
setattr(obj, self.field_name, obj.new_name) setattr(obj, self.field_name, obj.new_name)
obj.save() obj.save()