From 8a594d0a49ff2290b4b9f0fc0a8be3fe788cd58d Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 29 Jul 2024 15:21:43 -0400 Subject: [PATCH] Add validation for related_object_filter --- netbox/extras/models/customfields.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 4ca16bfb7..79ba75098 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -381,6 +381,17 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel): .format(type=self.get_type_display()) }) + # Related object filter can be set only for object-type fields, and must contain a dictionary mapping (if set) + if self.related_object_filter is not None: + if self.type not in (CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT): + raise ValidationError({ + 'related_object_filter': _("A related object filter can be defined only for object fields.") + }) + if type(self.related_object_filter) is not dict: + raise ValidationError({ + 'related_object_filter': _("Filter must be defined as a dictionary mapping attributes to values.") + }) + def serialize(self, value): """ Prepare a value for storage as JSON data.