Skip to content

Commit

Permalink
Merge pull request #2993 from linovia/bug/2894
Browse files Browse the repository at this point in the history
MultipleChoiceField empties incorrectly on a partial update using multipart/form-data (#2894)
  • Loading branch information
xordoquy committed Jun 1, 2015
2 parents 14055dd + 5c90bf9 commit f8eacc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,11 @@ def get_value(self, dictionary):
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
return dictionary.getlist(self.field_name)
ret = dictionary.getlist(self.field_name)
if getattr(self.root, 'partial', False) and not ret:
ret = empty
return ret

return dictionary.get(self.field_name, empty)

def to_internal_value(self, data):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal
from django.utils import timezone
from rest_framework import serializers
import rest_framework
import datetime
import django
import pytest
Expand Down Expand Up @@ -1038,6 +1039,15 @@ class TestMultipleChoiceField(FieldValues):
]
)

def test_against_partial_and_full_updates(self):
# serializer = self.Serializer(data=MockHTMLDict())
from django.http import QueryDict
field = serializers.MultipleChoiceField(choices=(('a', 'a'), ('b', 'b')))
field.partial = False
assert field.get_value(QueryDict({})) == []
field.partial = True
assert field.get_value(QueryDict({})) == rest_framework.fields.empty


# File serializers...

Expand Down

0 comments on commit f8eacc5

Please sign in to comment.