Skip to content

Fix DictField partial HTML omission handling#9906

Open
Sebastianhayashi wants to merge 1 commit intoencode:mainfrom
Sebastianhayashi:codex/fix-6234
Open

Fix DictField partial HTML omission handling#9906
Sebastianhayashi wants to merge 1 commit intoencode:mainfrom
Sebastianhayashi:codex/fix-6234

Conversation

@Sebastianhayashi
Copy link

Summary

Fix DictField handling for HTML form input on partial updates.

When the field is omitted in multipart/HTML input, DictField.get_value() currently returns {} and the serializer incorrectly treats the field as provided. This change returns empty for partial updates when parsed HTML dict input is empty, so omitted fields stay omitted.

Reproduction

from django.http import QueryDict
from rest_framework import serializers

class S(serializers.Serializer):
    field_name = serializers.DictField(required=False)

s = S(data=QueryDict(''), partial=True)
s.is_valid()
print(s.validated_data)  # before: {'field_name': {}}, after: {}

Changes

  • rest_framework/fields.py
    • In DictField.get_value(), for HTML input:
      • parse value via parse_html_dict
      • if value is empty and self.root.partial is true, return empty
  • tests/test_fields.py
    • Added regression test: test_partial_update_does_not_include_missing_html_dict_field

Validation

pytest -q \
  tests/test_fields.py::TestDictField::test_partial_update_does_not_include_missing_html_dict_field \
  tests/test_fields.py::TestDictField::test_allow_empty_disallowed \
  tests/test_fields.py::TestMultipleChoiceField::test_against_partial_and_full_updates

Result: 3 passed

Related to #6234.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Django REST framework’s DictField behavior for HTML/multipart inputs during partial updates so that an omitted dict field is treated as “missing” (sentinel empty) rather than an empty dict, preventing it from incorrectly appearing in validated_data.

Changes:

  • Updated DictField.get_value() to return empty when HTML parsing yields no keys during partial updates.
  • Added a regression test covering the partial-update omission case for HTML input.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
rest_framework/fields.py Treats empty parsed HTML dict input as omitted (empty) when partial=True.
tests/test_fields.py Adds regression test ensuring omitted HTML dict fields do not appear in validated_data on partial updates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants