Sfoglia il codice sorgente

Add object_type validation

jeremystretch 4 anni fa
parent
commit
bfc695434c
1 ha cambiato i file con 11 aggiunte e 0 eliminazioni
  1. 11 0
      netbox/extras/models/customfields.py

+ 11 - 0
netbox/extras/models/customfields.py

@@ -242,6 +242,17 @@ class CustomField(ChangeLoggedModel):
                 'default': f"The specified default value ({self.default}) is not listed as an available choice."
             })
 
+        # Object fields must define an object_type; other fields must not
+        if self.type in (CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT):
+            if not self.object_type:
+                raise ValidationError({
+                    'object_type': "Object fields must define an object type."
+                })
+        elif self.object_type:
+            raise ValidationError({
+                'object_type': f"{self.get_type_display()} fields may not define an object type."
+            })
+
     def serialize(self, value):
         """
         Prepare a value for storage as JSON data.