[PR #21040] [CLOSED] Fix nested serializer validation for PATCH requests with {"id": X} format #16128

Closed
opened 2025-12-30 00:25:57 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/21040
Author: @casibbald
Created: 12/26/2025
Status: Closed

Base: mainHead: fix/nested-serializer-tenant-validation


📝 Commits (1)

  • 1189ef8 Fix nested serializer validation for PATCH requests with {"id": X} format

📊 Changes

1 file changed (+13 additions, -0 deletions)

View changed files

📝 netbox/netbox/api/serializers/base.py (+13 -0)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/21040 **Author:** [@casibbald](https://github.com/casibbald) **Created:** 12/26/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/nested-serializer-tenant-validation` --- ### 📝 Commits (1) - [`1189ef8`](https://github.com/netbox-community/netbox/commit/1189ef8ac9c81fa4c0480710bd48e2e6d1c539d4) Fix nested serializer validation for PATCH requests with {"id": X} format ### 📊 Changes **1 file changed** (+13 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `netbox/netbox/api/serializers/base.py` (+13 -0) </details> ### 📄 Description 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. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:25:57 +01:00
adam closed this issue 2025-12-30 00:25:57 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#16128