From b46a89640f0dca7cab242f6d9435f7a32aa21ecd Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 23 Sep 2024 17:04:18 -0400 Subject: [PATCH] Closes #17577: Reference ModelState when determining whether an object is being created --- netbox/dcim/models/cables.py | 2 +- netbox/dcim/models/device_component_templates.py | 2 +- netbox/dcim/models/device_components.py | 6 +++--- netbox/dcim/models/devices.py | 6 +++--- netbox/dcim/models/racks.py | 4 ++-- netbox/netbox/models/__init__.py | 2 +- netbox/virtualization/models/clusters.py | 2 +- netbox/virtualization/models/virtualmachines.py | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index 2fac55dd4..9da599109 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -164,7 +164,7 @@ class Cable(PrimaryModel): if self.length is not None and not self.length_unit: raise ValidationError(_("Must specify a unit when setting a cable length")) - if self.pk is None and (not self.a_terminations or not self.b_terminations): + if self._state.adding and (not self.a_terminations or not self.b_terminations): raise ValidationError(_("Must define A and B terminations when creating a new cable.")) if self._terminations_modified: diff --git a/netbox/dcim/models/device_component_templates.py b/netbox/dcim/models/device_component_templates.py index 28a403be0..5f6aa08e3 100644 --- a/netbox/dcim/models/device_component_templates.py +++ b/netbox/dcim/models/device_component_templates.py @@ -98,7 +98,7 @@ class ComponentTemplateModel(ChangeLoggedModel, TrackingModelMixin): def clean(self): super().clean() - if self.pk is not None and self._original_device_type != self.device_type_id: + if not self._state.adding and self._original_device_type != self.device_type_id: raise ValidationError({ "device_type": _("Component templates cannot be moved to a different device type.") }) diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index 62312cbf4..f5fbaa956 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -561,7 +561,7 @@ class BaseInterface(models.Model): self.untagged_vlan = None # Only "tagged" interfaces may have tagged VLANs assigned. ("tagged all" implies all VLANs are assigned.) - if self.pk and self.mode != InterfaceModeChoices.MODE_TAGGED: + if not self._state.adding and self.mode != InterfaceModeChoices.MODE_TAGGED: self.tagged_vlans.clear() return super().save(*args, **kwargs) @@ -1072,7 +1072,7 @@ class RearPort(ModularComponentModel, CabledObjectModel, TrackingModelMixin): super().clean() # Check that positions count is greater than or equal to the number of associated FrontPorts - if self.pk: + if not self._state.adding: frontport_count = self.frontports.count() if self.positions < frontport_count: raise ValidationError({ @@ -1314,7 +1314,7 @@ class InventoryItem(MPTTModel, ComponentModel, TrackingModelMixin): }) # Validation for moving InventoryItems - if self.pk: + if not self._state.adding: # Cannot move an InventoryItem to another device if it has a parent if self.parent and self.parent.device != self.device: raise ValidationError({ diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index c281e5de2..a2b78957e 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -293,7 +293,7 @@ class DeviceType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): # If editing an existing DeviceType to have a larger u_height, first validate that *all* instances of it have # room to expand within their racks. This validation will impose a very high performance penalty when there are # many instances to check, but increasing the u_height of a DeviceType should be a very rare occurrence. - if self.pk and self.u_height > self._original_u_height: + if not self._state.adding and self.u_height > self._original_u_height: for d in Device.objects.filter(device_type=self, position__isnull=False): face_required = None if self.is_full_depth else d.face u_available = d.rack.get_available_units( @@ -310,7 +310,7 @@ class DeviceType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): }) # If modifying the height of an existing DeviceType to 0U, check for any instances assigned to a rack position. - elif self.pk and self._original_u_height > 0 and self.u_height == 0: + elif not self._state.adding and self._original_u_height > 0 and self.u_height == 0: racked_instance_count = Device.objects.filter( device_type=self, position__isnull=False @@ -1351,7 +1351,7 @@ class VirtualChassis(PrimaryModel): # Verify that the selected master device has been assigned to this VirtualChassis. (Skip when creating a new # VirtualChassis.) - if self.pk and self.master and self.master not in self.members.all(): + if not self._state.adding and self.master and self.master not in self.members.all(): raise ValidationError({ 'master': _("The selected master ({master}) is not assigned to this virtual chassis.").format( master=self.master diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index 3198c2679..3aead09ca 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -382,7 +382,7 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase): if self.max_weight and not self.weight_unit: raise ValidationError(_("Must specify a unit when setting a maximum weight")) - if self.pk: + if not self._state.adding: mounted_devices = Device.objects.filter(rack=self).exclude(position__isnull=True).order_by('position') # Validate that Rack is tall enough to house the highest mounted Device @@ -468,7 +468,7 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase): } # Add devices to rack units list - if self.pk: + if not self._state.adding: # Retrieve all devices installed within the rack devices = Device.objects.prefetch_related( diff --git a/netbox/netbox/models/__init__.py b/netbox/netbox/models/__init__.py index 4ba5f60da..aea5f00cc 100644 --- a/netbox/netbox/models/__init__.py +++ b/netbox/netbox/models/__init__.py @@ -162,7 +162,7 @@ class NestedGroupModel(NetBoxFeatureSet, MPTTModel): super().clean() # An MPTT model cannot be its own parent - if self.pk and self.parent and self.parent in self.get_descendants(include_self=True): + if not self._state.adding and self.parent and self.parent in self.get_descendants(include_self=True): raise ValidationError({ "parent": "Cannot assign self or child {type} as parent.".format(type=self._meta.verbose_name) }) diff --git a/netbox/virtualization/models/clusters.py b/netbox/virtualization/models/clusters.py index f8acc4c36..7f00d72a7 100644 --- a/netbox/virtualization/models/clusters.py +++ b/netbox/virtualization/models/clusters.py @@ -134,7 +134,7 @@ class Cluster(ContactsMixin, PrimaryModel): super().clean() # If the Cluster is assigned to a Site, verify that all host Devices belong to that Site. - if self.pk and self.site: + if not self._state.adding and self.site: if nonsite_devices := Device.objects.filter(cluster=self).exclude(site=self.site).count(): raise ValidationError({ 'site': _( diff --git a/netbox/virtualization/models/virtualmachines.py b/netbox/virtualization/models/virtualmachines.py index 2a63a4547..17929f476 100644 --- a/netbox/virtualization/models/virtualmachines.py +++ b/netbox/virtualization/models/virtualmachines.py @@ -205,7 +205,7 @@ class VirtualMachine(ContactsMixin, ImageAttachmentsMixin, RenderConfigMixin, Co }) # Validate aggregate disk size - if self.pk: + if not self._state.adding: total_disk = self.virtualdisks.aggregate(Sum('size', default=0))['size__sum'] if total_disk and self.disk is None: self.disk = total_disk