Fix nested serializer validation for PATCH requests with {"id": X} format #682

Closed
opened 2026-04-05 17:09:17 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @casibbald on 12/26/2025

When a nested serializer receives {"id": X} in a PATCH request, DRF was attempting to validate it as a full object, requiring all required fields (e.g., 'name' and 'group' for Tenant). This caused validation errors like 'group and name are required' even though the nested serializer should only look up the object by ID.

The fix overrides run_validation() to skip field-level validation for nested serializers, allowing to_internal_value() to properly handle {"id": X} format by looking up the object via get_related_object_by_attrs().

Fixes issue where PATCH requests to update Site with tenant field using {"id": X} format would fail with validation errors.

*Originally created by @casibbald on 12/26/2025* When a nested serializer receives {"id": X} in a PATCH request, DRF was attempting to validate it as a full object, requiring all required fields (e.g., 'name' and 'group' for Tenant). This caused validation errors like 'group and name are required' even though the nested serializer should only look up the object by ID. The fix overrides run_validation() to skip field-level validation for nested serializers, allowing to_internal_value() to properly handle {"id": X} format by looking up the object via get_related_object_by_attrs(). Fixes issue where PATCH requests to update Site with tenant field using {"id": X} format would fail with validation errors. <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be approved and assigned prior to opening a pull request. This helps avoid waste time and effort on a proposed change that we might not be able to accept. IF YOUR PULL REQUEST DOES NOT REFERENCE AN ISSUE WHICH HAS BEEN ASSIGNED TO YOU, IT WILL BE CLOSED AUTOMATICALLY. Please specify your assigned issue number on the line below. --> <!-- Please include a summary of the proposed changes below. -->
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#682