Skip to content

Commit 8abaefa

Browse files
authored
Merge pull request #164 from netboxlabs/NPL-411-reserved-names
NPL-411 raise validation error if using reserved names for fields
2 parents 962de13 + c111f86 commit 8abaefa

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

netbox_custom_objects/constants.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@
66
)
77

88
APP_LABEL = "netbox_custom_objects"
9+
10+
# Field names that are reserved and cannot be used for custom object fields
11+
RESERVED_FIELD_NAMES = [
12+
"bookmarks",
13+
"contacts",
14+
"created",
15+
"custom_field_data",
16+
"id",
17+
"images",
18+
"jobs",
19+
"journal_entries",
20+
"last_updated",
21+
"pk",
22+
"subscriptions",
23+
"tags",
24+
]

netbox_custom_objects/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from utilities.string import title
5050
from utilities.validators import validate_regex
5151

52-
from netbox_custom_objects.constants import APP_LABEL
52+
from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES
5353
from netbox_custom_objects.field_types import FIELD_TYPE_CLASS
5454

5555

@@ -813,6 +813,16 @@ def related_object_type_label(self):
813813
def clean(self):
814814
super().clean()
815815

816+
# Check if the field name is reserved
817+
if self.name in RESERVED_FIELD_NAMES:
818+
raise ValidationError(
819+
{
820+
"name": _(
821+
'Field name "{name}" is reserved and cannot be used. Reserved names are: {reserved_names}'
822+
).format(name=self.name, reserved_names=", ".join(RESERVED_FIELD_NAMES))
823+
}
824+
)
825+
816826
# Validate the field's default value (if any)
817827
if self.default is not None:
818828
try:

0 commit comments

Comments
 (0)