Просмотр исходного кода

Fixes #3983: Permit the creation of multiple unnamed devices

Jeremy Stretch 6 лет назад
Родитель
Сommit
b7dea5a9f7

+ 8 - 0
docs/release-notes/version-2.7.md

@@ -1,3 +1,11 @@
+# v2.7.3 (FUTURE)
+
+## Bug Fixes
+
+* [#3983](https://github.com/netbox-community/netbox/issues/3983) - Permit the creation of multiple unnamed devices
+
+---
+
 # v2.7.2 (2020-01-21)
 
 ## Enhancements

+ 5 - 4
netbox/dcim/models/__init__.py

@@ -1445,10 +1445,11 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
         # 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.tenant is None and Device.objects.exclude(pk=self.pk).filter(name=self.name, tenant__isnull=True):
-            raise ValidationError({
-                'name': 'A device with this name already exists.'
-            })
+        if self.name and self.tenant is None:
+            if Device.objects.exclude(pk=self.pk).filter(name=self.name, tenant__isnull=True):
+                raise ValidationError({
+                    'name': 'A device with this name already exists.'
+                })
 
         super().validate_unique(exclude)
 

+ 22 - 1
netbox/dcim/tests/test_models.py

@@ -285,7 +285,28 @@ class DeviceTestCase(TestCase):
             name='Device Bay 1'
         )
 
-    def test_device_duplicate_name_per_site(self):
+    def test_multiple_unnamed_devices(self):
+
+        device1 = Device(
+            site=self.site,
+            device_type=self.device_type,
+            device_role=self.device_role,
+            name=''
+        )
+        device1.save()
+
+        device2 = Device(
+            site=device1.site,
+            device_type=device1.device_type,
+            device_role=device1.device_role,
+            name=''
+        )
+        device2.full_clean()
+        device2.save()
+
+        self.assertEqual(Device.objects.filter(name='').count(), 2)
+
+    def test_device_duplicate_names(self):
 
         device1 = Device(
             site=self.site,