From 989c08109bf0913b43a16ec7f951bbb6f6f6bef6 Mon Sep 17 00:00:00 2001 From: Xavier Ordoquy Date: Mon, 1 Jun 2015 16:04:05 +0100 Subject: [PATCH 1/3] Failing test case for #2894 --- tests/test_fields.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index 568e8d5e7..c4c2eeeca 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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 @@ -1017,6 +1018,12 @@ class TestMultipleChoiceField(FieldValues): ] ) + def test_against_partial_updates(self): + # serializer = self.Serializer(data=MockHTMLDict()) + from django.http import QueryDict + field = serializers.MultipleChoiceField(choices=(('a', 'a'), ('b', 'b'))) + assert field.get_value(QueryDict({})) == rest_framework.fields.empty + # File serializers... From 94e2d3ca610ec1fdb97641a094801f69a1ab2613 Mon Sep 17 00:00:00 2001 From: Xavier Ordoquy Date: Mon, 1 Jun 2015 16:13:12 +0100 Subject: [PATCH 2/3] Test case upgrade to use partial data --- tests/test_fields.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index c4c2eeeca..0867852be 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1018,10 +1018,13 @@ class TestMultipleChoiceField(FieldValues): ] ) - def test_against_partial_updates(self): + 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 From 5c90bf9cc00e9870ba1d1d5bd3113ce797e73306 Mon Sep 17 00:00:00 2001 From: Xavier Ordoquy Date: Mon, 1 Jun 2015 16:13:35 +0100 Subject: [PATCH 3/3] Fix for #2894 thanks to @carljm --- rest_framework/fields.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d8bb0a017..e7a4cee5f 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1060,7 +1060,11 @@ class MultipleChoiceField(ChoiceField): # 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):