mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #12133: Move any instance mutations inside clean() to save()
This commit is contained in:
@@ -169,8 +169,6 @@ class VirtualMachine(PrimaryModel, ConfigContextModel):
|
||||
raise ValidationError({
|
||||
'cluster': f'The selected cluster ({self.cluster}) is not assigned to this site ({self.site}).'
|
||||
})
|
||||
elif self.cluster:
|
||||
self.site = self.cluster.site
|
||||
|
||||
# Validate assigned cluster device
|
||||
if self.device and not self.cluster:
|
||||
@@ -201,6 +199,14 @@ class VirtualMachine(PrimaryModel, ConfigContextModel):
|
||||
field: f"The specified IP address ({ip}) is not assigned to this VM.",
|
||||
})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
|
||||
# Assign site from cluster if not set
|
||||
if self.cluster and not self.site:
|
||||
self.site = self.cluster.site
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_status_color(self):
|
||||
return VirtualMachineStatusChoices.colors.get(self.status)
|
||||
|
||||
|
@@ -72,7 +72,7 @@ class VirtualMachineTestCase(TestCase):
|
||||
|
||||
# VM with cluster site but no direct site should have its site set automatically
|
||||
vm = VirtualMachine(name='vm1', site=None, cluster=clusters[0])
|
||||
vm.full_clean()
|
||||
vm.save()
|
||||
self.assertEqual(vm.site, sites[0])
|
||||
|
||||
def test_vm_name_case_sensitivity(self):
|
||||
|
Reference in New Issue
Block a user