mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Remove custom validate_unique() methods
This commit is contained in:
@@ -651,10 +651,25 @@ class Device(NetBoxModel, ConfigContextModel):
|
||||
|
||||
class Meta:
|
||||
ordering = ('_name', 'pk') # Name may be null
|
||||
unique_together = (
|
||||
('site', 'tenant', 'name'), # See validate_unique below
|
||||
('rack', 'position', 'face'),
|
||||
('virtual_chassis', 'vc_position'),
|
||||
constraints = (
|
||||
models.UniqueConstraint(
|
||||
name='dcim_device_unique_name_site_tenant',
|
||||
fields=('name', 'site', 'tenant')
|
||||
),
|
||||
models.UniqueConstraint(
|
||||
name='dcim_device_unique_name_site',
|
||||
fields=('name', 'site'),
|
||||
condition=Q(tenant__isnull=True),
|
||||
violation_error_message="Device name must be unique per site."
|
||||
),
|
||||
models.UniqueConstraint(
|
||||
name='dcim_device_unique_rack_position_face',
|
||||
fields=('rack', 'position', 'face')
|
||||
),
|
||||
models.UniqueConstraint(
|
||||
name='dcim_device_unique_virtual_chassis_vc_position',
|
||||
fields=('virtual_chassis', 'vc_position')
|
||||
),
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
@@ -679,23 +694,6 @@ class Device(NetBoxModel, ConfigContextModel):
|
||||
def get_absolute_url(self):
|
||||
return reverse('dcim:device', args=[self.pk])
|
||||
|
||||
def validate_unique(self, exclude=None):
|
||||
|
||||
# Check for a duplicate name on a device assigned to the same Site and no Tenant. This is necessary
|
||||
# because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
|
||||
# of the uniqueness constraint without manual intervention.
|
||||
if self.name and hasattr(self, 'site') and self.tenant is None:
|
||||
if Device.objects.exclude(pk=self.pk).filter(
|
||||
name=self.name,
|
||||
site=self.site,
|
||||
tenant__isnull=True
|
||||
):
|
||||
raise ValidationError({
|
||||
'name': 'A device with this name already exists.'
|
||||
})
|
||||
|
||||
super().validate_unique(exclude)
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
|
||||
|
Reference in New Issue
Block a user