mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-22 11:38:45 -06:00
use bulk_update and rebuild
This commit is contained in:
@@ -330,54 +330,35 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
|
|||||||
component._location = self.device.location
|
component._location = self.device.location
|
||||||
component._rack = self.device.rack
|
component._rack = self.device.rack
|
||||||
|
|
||||||
if issubclass(component_model, MPTTModel):
|
component_model.objects.bulk_create(create_instances)
|
||||||
# MPTT models must be saved individually to maintain tree structure
|
# Emit the post_save signal for each newly created object
|
||||||
# Use delay_mptt_updates for better performance
|
for component in create_instances:
|
||||||
with component_model.objects.delay_mptt_updates():
|
post_save.send(
|
||||||
for instance in create_instances:
|
sender=component_model,
|
||||||
instance.save()
|
instance=component,
|
||||||
else:
|
created=True,
|
||||||
component_model.objects.bulk_create(create_instances)
|
raw=False,
|
||||||
# Emit the post_save signal for each newly created object
|
using='default',
|
||||||
for component in create_instances:
|
update_fields=None
|
||||||
post_save.send(
|
)
|
||||||
sender=component_model,
|
|
||||||
instance=component,
|
|
||||||
created=True,
|
|
||||||
raw=False,
|
|
||||||
using='default',
|
|
||||||
update_fields=None
|
|
||||||
)
|
|
||||||
|
|
||||||
update_fields = ['module']
|
update_fields = ['module']
|
||||||
|
|
||||||
if issubclass(component_model, MPTTModel):
|
component_model.objects.bulk_update(update_instances, update_fields)
|
||||||
# MPTT models must be saved individually to maintain tree structure
|
# Emit the post_save signal for each updated object
|
||||||
# Use delay_mptt_updates for better performance - could do bulk_update
|
for component in update_instances:
|
||||||
# but then would need to rebuild the tree after the updates.
|
post_save.send(
|
||||||
with component_model.objects.delay_mptt_updates():
|
sender=component_model,
|
||||||
for component in update_instances:
|
instance=component,
|
||||||
component.save()
|
created=False,
|
||||||
post_save.send(
|
raw=False,
|
||||||
sender=component_model,
|
using='default',
|
||||||
instance=component,
|
update_fields=update_fields
|
||||||
created=False,
|
)
|
||||||
raw=False,
|
|
||||||
using='default',
|
# Rebuild MPTT tree if needed (bulk operations bypass model save)
|
||||||
update_fields=update_fields
|
if issubclass(component_model, MPTTModel) and (create_instances or update_instances):
|
||||||
)
|
component_model.objects.rebuild()
|
||||||
else:
|
|
||||||
component_model.objects.bulk_update(update_instances, update_fields)
|
|
||||||
# Emit the post_save signal for each updated object
|
|
||||||
for component in update_instances:
|
|
||||||
post_save.send(
|
|
||||||
sender=component_model,
|
|
||||||
instance=component,
|
|
||||||
created=False,
|
|
||||||
raw=False,
|
|
||||||
using='default',
|
|
||||||
update_fields=update_fields
|
|
||||||
)
|
|
||||||
|
|
||||||
# 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user