Merge pull request #22850 from netbox-community/22844-CustomFieldChoiceSetSerializer-base_choices

Fixes #22844: Allow null value for CustomFieldChoiceSet `base_choices` in REST API
This commit is contained in:
bctiemann 2026-08-04 13:54:25 -04:00 committed by GitHub
commit 8b203e55a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View File

@ -19,7 +19,8 @@ __all__ = (
class CustomFieldChoiceSetSerializer(OwnerMixin, ChangeLogMessageSerializer, ValidatedModelSerializer):
base_choices = ChoiceField(
choices=CustomFieldChoiceSetBaseChoices,
required=False
required=False,
allow_null=True,
)
extra_choices = serializers.ListField(
child=serializers.ListField(

View File

@ -296,6 +296,36 @@ class CustomFieldChoiceSetTestCase(APIViewTestCases.APIViewTestCase):
response = self.client.post(self._get_list_url(), data, format='json', **self.header)
self.assertEqual(response.status_code, 400)
def test_null_base_choices(self):
"""
A null value for base_choices should be accepted, as returned by the API for a choice set which defines
only extra choices.
"""
self.add_permissions('extras.add_customfieldchoiceset', 'extras.change_customfieldchoiceset')
data = {
'name': 'test',
'base_choices': None,
'extra_choices': [
['choice1', 'Choice 1'],
],
}
response = self.client.post(self._get_list_url(), data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertIsNone(response.data['base_choices'])
choice_set = CustomFieldChoiceSet.objects.get(pk=response.data['id'])
self.assertIsNone(choice_set.base_choices)
# A choice set with base choices assigned can be reverted to null
choice_set.base_choices = CustomFieldChoiceSetBaseChoices.IATA
choice_set.save()
response = self.client.patch(
self._get_detail_url(choice_set), {'base_choices': None}, format='json', **self.header
)
self.assertHttpStatus(response, status.HTTP_200_OK)
choice_set.refresh_from_db()
self.assertIsNone(choice_set.base_choices)
def test_invalid_choice_color(self):
self.add_permissions('extras.add_customfieldchoiceset')
data = {