From bc26529be86d2849173765fa74360089d6cb2e8e Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 5 Jan 2026 09:49:32 -0500 Subject: [PATCH 1/2] Fixes #21049: Remove stale custom field data during object validation --- netbox/netbox/models/features.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index e0d03d6e7..653dc50c8 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -288,12 +288,13 @@ class CustomFieldsMixin(models.Model): cf.name: cf for cf in CustomField.objects.get_for_model(self) } + # Remove any stale custom field data + self.custom_field_data = { + k: v for k, v in self.custom_field_data.items() if k in custom_fields.keys() + } + # Validate all field values for field_name, value in self.custom_field_data.items(): - if field_name not in custom_fields: - raise ValidationError(_("Unknown field name '{name}' in custom field data.").format( - name=field_name - )) try: custom_fields[field_name].validate(value) except ValidationError as e: From 6ed6da49d9acd096eba36f4946592c8a6a415763 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 5 Jan 2026 11:00:54 -0500 Subject: [PATCH 2/2] Update test --- netbox/extras/tests/test_customfields.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/netbox/extras/tests/test_customfields.py b/netbox/extras/tests/test_customfields.py index 502759ab9..a8b65f119 100644 --- a/netbox/extras/tests/test_customfields.py +++ b/netbox/extras/tests/test_customfields.py @@ -1506,19 +1506,18 @@ class CustomFieldModelTest(TestCase): def test_invalid_data(self): """ - Setting custom field data for a non-applicable (or non-existent) CustomField should raise a ValidationError. + Any invalid or stale custom field data should be removed from the instance. """ site = Site(name='Test Site', slug='test-site') # Set custom field data site.custom_field_data['foo'] = 'abc' site.custom_field_data['bar'] = 'def' - with self.assertRaises(ValidationError): - site.clean() - - del site.custom_field_data['bar'] site.clean() + self.assertIn('foo', site.custom_field_data) + self.assertNotIn('bar', site.custom_field_data) + def test_missing_required_field(self): """ Check that a ValidationError is raised if any required custom fields are not present.