Skip to content

Commit c5a04a8

Browse files
committed
Add test for nullable ChoiceField and blank HTML input. Closes #2623.
1 parent 75beb6a commit c5a04a8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tests/test_fields.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,22 @@ def test_allow_blank(self):
10701070
output = field.run_validation('')
10711071
assert output == ''
10721072

1073+
def test_allow_null(self):
1074+
"""
1075+
If `allow_null=True` then '' on HTML forms is treated as None.
1076+
"""
1077+
field = serializers.ChoiceField(
1078+
allow_null=True,
1079+
choices=[
1080+
1, 2, 3
1081+
]
1082+
)
1083+
field.field_name = 'example'
1084+
value = field.get_value(QueryDict('example='))
1085+
assert value is None
1086+
output = field.run_validation(None)
1087+
assert output is None
1088+
10731089

10741090
class TestChoiceFieldWithType(FieldValues):
10751091
"""

0 commit comments

Comments
 (0)