fix(dcim): Improve validation checks for rack attributes

Replaces direct attribute access with `getattr` to avoid runtime
errors when fields are missing. Updates validation for site, location,
and rack dimension attributes to enhance robustness.

Fixes #21320
This commit is contained in:
Martin Hauser
2026-01-29 14:57:43 +01:00
parent 8e620ef325
commit 8860f2c76d
+9 -3
View File
@@ -373,15 +373,21 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, TrackingModelMixin, RackBase):
super().clean() super().clean()
# Validate location/site assignment # Validate location/site assignment
if self.site and self.location and self.location.site != self.site: if getattr(self, 'site', None) and getattr(self, 'location', None) and self.location.site != self.site:
raise ValidationError(_("Assigned location must belong to parent site ({site}).").format(site=self.site)) raise ValidationError(_("Assigned location must belong to parent site ({site}).").format(site=self.site))
# Validate outer dimensions and unit # Validate outer dimensions and unit
if any([self.outer_width, self.outer_depth, self.outer_height]) and not self.outer_unit: if any(
[
getattr(self, 'outer_width', None),
getattr(self, 'outer_depth', None),
getattr(self, 'outer_height', None),
]
) and not getattr(self, 'outer_unit', None):
raise ValidationError(_("Must specify a unit when setting an outer dimension")) raise ValidationError(_("Must specify a unit when setting an outer dimension"))
# Validate max_weight and weight_unit # Validate max_weight and weight_unit
if self.max_weight and not self.weight_unit: if getattr(self, 'max_weight', None) and not getattr(self, 'weight_unit', None):
raise ValidationError(_("Must specify a unit when setting a maximum weight")) raise ValidationError(_("Must specify a unit when setting a maximum weight"))
if not self._state.adding: if not self._state.adding: