Skip to content

Commit 5406e8e

Browse files
committed
Optimize background site/location updates
1 parent d3974c9 commit 5406e8e

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

netbox/dcim/signals.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def rebuild_paths(obj):
3737

3838

3939
#
40-
# Site/rack/device assignment
40+
# Location/rack/device assignment
4141
#
4242

4343
@receiver(post_save, sender=Location)
@@ -47,18 +47,11 @@ def handle_location_site_change(instance, created, **kwargs):
4747
object instead of calling update() on the QuerySet to ensure the proper change records get created for each.
4848
"""
4949
if not created:
50-
for location in instance.get_children():
51-
location.site = instance.site
52-
location.save()
53-
for rack in Rack.objects.filter(location=instance).exclude(site=instance.site):
54-
rack.site = instance.site
55-
rack.save()
56-
for device in Device.objects.filter(location=instance).exclude(site=instance.site):
57-
device.site = instance.site
58-
device.save()
59-
for powerpanel in PowerPanel.objects.filter(location=instance).exclude(site=instance.site):
60-
powerpanel.site = instance.site
61-
powerpanel.save()
50+
instance.get_descendants().update(site=instance.site)
51+
locations = instance.get_descendants(include_self=True).values_list('pk', flat=True)
52+
Rack.objects.filter(location__in=locations).update(site=instance.site)
53+
Device.objects.filter(location__in=locations).update(site=instance.site)
54+
PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
6255

6356

6457
@receiver(post_save, sender=Rack)
@@ -67,10 +60,7 @@ def handle_rack_site_change(instance, created, **kwargs):
6760
Update child Devices if Site or Location assignment has changed.
6861
"""
6962
if not created:
70-
for device in Device.objects.filter(rack=instance).exclude(site=instance.site, location=instance.location):
71-
device.site = instance.site
72-
device.location = instance.location
73-
device.save()
63+
Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
7464

7565

7666
#

0 commit comments

Comments
 (0)