Fix allow_null=True fields skipped from validated_data when partial=False#9898
Closed
suportly wants to merge 1 commit intoencode:mainfrom
Closed
Fix allow_null=True fields skipped from validated_data when partial=False#9898suportly wants to merge 1 commit intoencode:mainfrom
suportly wants to merge 1 commit intoencode:mainfrom
Conversation
…tial=False When a field has allow_null=True and required=False but no explicit default, omitting it from input data with partial=False would silently skip it from validated_data, effectively allowing unintended partial updates. The fix catches SkipField from get_default() in validate_empty_values() and returns None for nullable fields, ensuring they appear in validated_data during full (non-partial) updates. Refs encode#9501 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where serializer fields with allow_null=True and required=False (but no explicit default) were being silently omitted from validated_data during full updates (partial=False), causing unintended partial update behavior. The fix ensures these nullable fields are included in validated_data with a None value during full updates, while preserving the existing behavior for partial updates.
Changes:
- Modified
Field.validate_empty_values()to catchSkipFieldexceptions and returnNonefor nullable fields during full updates - Added comprehensive test coverage with 6 new test cases covering create, update, partial update, explicit null, explicit default, and non-nullable scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rest_framework/fields.py | Added try-except block in validate_empty_values() to handle SkipField from get_default() and return (True, None) for fields with allow_null=True |
| tests/test_serializer.py | Added TestAllowNullNotRequiredInclusions test class with 6 comprehensive test cases validating the fix across different scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Thanks for raising this old issue to my attention again. Closing as per: #9501 (comment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes an issue where serializer fields with
allow_null=Trueandrequired=False(but no explicit default) are silently omitted fromvalidated_datawhenpartial=False, effectively allowing unintended partial updates.refs #9501
Root Cause
In
Field.validate_empty_values(), when input data is missing (empty),partial=False, andrequired=False, the method callsself.get_default(). Sinceallow_null=Truedoes not set an explicit default value,get_default()raisesSkipField, and the field is silently omitted fromvalidated_data— identical topartial=Truebehavior.Fix
In
validate_empty_values(), catchSkipFieldfromget_default()and return(True, None)when the field hasallow_null=True. This ensures nullable fields appear invalidated_dataduring full (non-partial) updates. For non-nullable fields, existing behavior is preserved (re-raiseSkipField).Test plan
TestAllowNullNotRequiredInclusions:NoneNoneNonerequired=False→ still skipped (preserved)🤖 Generated with Claude Code