diff --git a/netbox_custom_objects/constants.py b/netbox_custom_objects/constants.py index ef5bc60..5ae577e 100644 --- a/netbox_custom_objects/constants.py +++ b/netbox_custom_objects/constants.py @@ -6,3 +6,19 @@ ) APP_LABEL = "netbox_custom_objects" + +# Field names that are reserved and cannot be used for custom object fields +RESERVED_FIELD_NAMES = [ + "bookmarks", + "contacts", + "created", + "custom_field_data", + "id", + "images", + "jobs", + "journal_entries", + "last_updated", + "pk", + "subscriptions", + "tags", +] diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 9ec2d4c..fa2f160 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -49,7 +49,7 @@ from utilities.string import title from utilities.validators import validate_regex -from netbox_custom_objects.constants import APP_LABEL +from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES from netbox_custom_objects.field_types import FIELD_TYPE_CLASS USER_TABLE_DATABASE_NAME_PREFIX = "custom_objects_" @@ -799,6 +799,16 @@ def related_object_type_label(self): def clean(self): super().clean() + # Check if the field name is reserved + if self.name in RESERVED_FIELD_NAMES: + raise ValidationError( + { + "name": _( + 'Field name "{name}" is reserved and cannot be used. Reserved names are: {reserved_names}' + ).format(name=self.name, reserved_names=", ".join(RESERVED_FIELD_NAMES)) + } + ) + # Validate the field's default value (if any) if self.default is not None: try: