From c5a04a85169524d894df8bcc39fe3e6945868222 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 27 Jul 2015 13:51:03 +0100 Subject: [PATCH] Add test for nullable ChoiceField and blank HTML input. Closes #2623. --- tests/test_fields.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index 94eb5df79..52ccd6362 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1070,6 +1070,22 @@ class TestChoiceField(FieldValues): output = field.run_validation('') assert output == '' + def test_allow_null(self): + """ + If `allow_null=True` then '' on HTML forms is treated as None. + """ + field = serializers.ChoiceField( + allow_null=True, + choices=[ + 1, 2, 3 + ] + ) + field.field_name = 'example' + value = field.get_value(QueryDict('example=')) + assert value is None + output = field.run_validation(None) + assert output is None + class TestChoiceFieldWithType(FieldValues): """