Skip to content

Commit f8eacc5

Browse files
committed
Merge pull request #2993 from linovia/bug/2894
MultipleChoiceField empties incorrectly on a partial update using multipart/form-data (#2894)
2 parents 14055dd + 5c90bf9 commit f8eacc5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

rest_framework/fields.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,11 @@ def get_value(self, dictionary):
10831083
# We override the default field access in order to support
10841084
# lists in HTML forms.
10851085
if html.is_html_input(dictionary):
1086-
return dictionary.getlist(self.field_name)
1086+
ret = dictionary.getlist(self.field_name)
1087+
if getattr(self.root, 'partial', False) and not ret:
1088+
ret = empty
1089+
return ret
1090+
10871091
return dictionary.get(self.field_name, empty)
10881092

10891093
def to_internal_value(self, data):

tests/test_fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from decimal import Decimal
22
from django.utils import timezone
33
from rest_framework import serializers
4+
import rest_framework
45
import datetime
56
import django
67
import pytest
@@ -1038,6 +1039,15 @@ class TestMultipleChoiceField(FieldValues):
10381039
]
10391040
)
10401041

1042+
def test_against_partial_and_full_updates(self):
1043+
# serializer = self.Serializer(data=MockHTMLDict())
1044+
from django.http import QueryDict
1045+
field = serializers.MultipleChoiceField(choices=(('a', 'a'), ('b', 'b')))
1046+
field.partial = False
1047+
assert field.get_value(QueryDict({})) == []
1048+
field.partial = True
1049+
assert field.get_value(QueryDict({})) == rest_framework.fields.empty
1050+
10411051

10421052
# File serializers...
10431053

0 commit comments

Comments
 (0)