Skip to content

Conversation

@atownson
Copy link
Contributor

@atownson atownson commented Apr 8, 2025

Fixes: #18669

While it is possible to fix this issue by modifying CustomFieldsDataField.to_internal_value() as such:

    def to_internal_value(self, data):
        if type(data) is not dict:
            raise ValidationError(
                "Invalid data format. Custom field data must be passed as a dictionary mapping field names to their "
                "values."
            )

        for cf in self._get_custom_fields():
            # Apply default values if omitted in the request
            if cf.name not in data and cf.default is not None:
                data[cf.name] = cf.default
            # Serialize object and multi-object values
            elif cf.name in data and data[cf.name] not in CUSTOMFIELD_EMPTY_VALUES and cf.type in (
                    CustomFieldTypeChoices.TYPE_OBJECT,
                    CustomFieldTypeChoices.TYPE_MULTIOBJECT
            ):
                serializer_class = get_serializer_for_model(cf.related_object_type.model_class())
                many = cf.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT
                serializer = serializer_class(data=data[cf.name], nested=True, many=many, context=self.parent.context)
                if serializer.is_valid():
                    data[cf.name] = [obj['id'] for obj in serializer.data] if many else serializer.data['id']
                else:
                    raise ValidationError(_("Unknown related object(s): {name}").format(name=data[cf.name]))

        # If updating an existing instance, start with existing custom_field_data
        if self.parent.instance:
            data = {**self.parent.instance.custom_field_data, **data}

        return data

overriding the save() method on the mixin ensures the fix is applied to more than just the API endpoint (i.e. scripts). The populate_custom_field_defaults() method on the mixin is no longer needed but retained. It's function is to force the values of custom fields to the default rather than simply setting the default values if the fields are omitted. It was only used to populate newly created IP Addresses for FHRP groups. The save() method results are the same.

@jnovinger jnovinger requested review from a team and arthanson and removed request for a team April 8, 2025 19:05
Copy link
Collaborator

@arthanson arthanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @atownson

@arthanson arthanson merged commit c108c73 into netbox-community:main Apr 15, 2025
3 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default values for custom_fields are ignored in API when custom_fields section is used

2 participants