From fd309a0003a8442b4c635a311441dfe8b48a83c0 Mon Sep 17 00:00:00 2001 From: Alexander Haase Date: Mon, 16 Sep 2024 17:08:18 +0200 Subject: [PATCH] Clone device site and location from rack When a device is assigned to a rack, its site and location are cloned from the rack regardless of user input. This also allows some consistency checks to be removed, as inconsistent data would still be overwritten with valid values when saving the Device object. --- netbox/dcim/models/devices.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index c281e5de2..74fcdc37f 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -829,23 +829,13 @@ class Device( def clean(self): super().clean() - # Validate site/location/rack combination - if self.rack and self.site != self.rack.site: - raise ValidationError({ - 'rack': _("Rack {rack} does not belong to site {site}.").format(rack=self.rack, site=self.site), - }) + # Validate site/location combination if self.location and self.site != self.location.site: raise ValidationError({ 'location': _( "Location {location} does not belong to site {site}." ).format(location=self.location, site=self.site) }) - if self.rack and self.location and self.rack.location != self.location: - raise ValidationError({ - 'rack': _( - "Rack {rack} does not belong to location {location}." - ).format(rack=self.rack, location=self.location) - }) if self.rack is None: if self.face: @@ -1031,8 +1021,9 @@ class Device( if is_new and not self.platform: self.platform = self.device_type.default_platform - # Inherit location from Rack if not set - if self.rack and self.rack.location: + # Inherit site/location from Rack + if self.rack: + self.site = self.rack.site self.location = self.rack.location super().save(*args, **kwargs)