Skip to content

Commit ec13a79

Browse files
authored
Fixes #20875: Fix updating of denormalized fields for component models (#20956)
1 parent 21f4036 commit ec13a79

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

netbox/dcim/models/devices.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ def _instantiate_components(self, queryset, bulk_create=True):
957957
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
958958
for component in components:
959959
component.custom_field_data = cf_defaults
960+
# Set denormalized references
961+
for component in components:
962+
component._site = self.site
963+
component._location = self.location
964+
component._rack = self.rack
960965
components = model.objects.bulk_create(components)
961966
# Prefetch related objects to minimize queries needed during post_save
962967
prefetch_fields = get_prefetchable_fields(model)

netbox/dcim/models/modules.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ def save(self, *args, **kwargs):
315315
for component in create_instances:
316316
component.custom_field_data = cf_defaults
317317

318+
# Set denormalized references
319+
for component in create_instances:
320+
component._site = self.device.site
321+
component._location = self.device.location
322+
component._rack = self.device.rack
323+
318324
if component_model is not ModuleBay:
319325
component_model.objects.bulk_create(create_instances)
320326
# Emit the post_save signal for each newly created object

netbox/dcim/signals.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def handle_location_site_change(instance, created, **kwargs):
4444
Device.objects.filter(location__in=locations).update(site=instance.site)
4545
PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
4646
CableTermination.objects.filter(_location__in=locations).update(_site=instance.site)
47+
# Update component models for devices in these locations
48+
for model in COMPONENT_MODELS:
49+
model.objects.filter(device__location__in=locations).update(_site=instance.site)
4750

4851

4952
@receiver(post_save, sender=Rack)
@@ -53,6 +56,12 @@ def handle_rack_site_change(instance, created, **kwargs):
5356
"""
5457
if not created:
5558
Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
59+
# Update component models for devices in this rack
60+
for model in COMPONENT_MODELS:
61+
model.objects.filter(device__rack=instance).update(
62+
_site=instance.site,
63+
_location=instance.location,
64+
)
5665

5766

5867
@receiver(post_save, sender=Device)

0 commit comments

Comments
 (0)