Fix DictField HTML input returning empty dict for missing fields#9891
Open
veeceey wants to merge 1 commit intoencode:mainfrom
Open
Fix DictField HTML input returning empty dict for missing fields#9891veeceey wants to merge 1 commit intoencode:mainfrom
veeceey wants to merge 1 commit intoencode:mainfrom
Conversation
…input (encode#6234) When using DictField with HTML form (multipart/form-data) input, parse_html_dict always returned an empty MultiValueDict when no matching keys were found. This made it impossible to distinguish between an unspecified field and an empty input, causing issues with required/default field handling. This aligns parse_html_dict with parse_html_list by adding a default parameter that is returned when no matching keys are found. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #6234
parse_html_dictalways returned an emptyMultiValueDictwhen no matching keys were found in the HTML form input, making it impossible to distinguish between:This caused
DictFieldto treat missing fields as if they contained an empty dict, breakingrequired,default, and partial update logic formultipart/form-datarequests.Changes
rest_framework/utils/html.py: Added adefaultparameter toparse_html_dict(consistent with howparse_html_listalready works). Returnsdefaultwhen no matching keys are found in the input.rest_framework/fields.py: UpdatedDictField.get_valueto passdefault=emptyso that missing fields are correctly treated as not provided. Also updatedDictField.to_internal_valueto passdefault=dataso inner MultiValueDict data is preserved when no dot-separated sub-keys are found.rest_framework/serializers.py: UpdatedSerializer.get_valueto use the newdefault=parameter instead ofor emptyfor consistency.tests/test_fields.py: Added 4 new test cases for DictField HTML form input covering:Test plan
test_fields.py,test_serializer.py, andtest_parsers.pysuites pass (401 passed, 1 pre-existing failure unrelated to this change)multipart/form-datarequests that DictField partial updates work correctly🤖 Generated with Claude Code