Merge pull request #3544 from dursk/nested-as-form-fields

Correctly handle False in NestedBoundField.as_form_field()
This commit is contained in:
Tom Christie 2015-10-27 16:55:36 +00:00
commit fa6f053323
2 changed files with 24 additions and 1 deletions

View File

@ -112,7 +112,7 @@ class NestedBoundField(BoundField):
if isinstance(value, (list, dict)):
values[key] = value
else:
values[key] = '' if value is None else force_text(value)
values[key] = '' if (value is None or value is False) else force_text(value)
return self.__class__(self._field, values, self.errors, self._prefix)

View File

@ -45,6 +45,16 @@ class TestSimpleBoundField:
assert serializer['amount'].errors is None
assert serializer['amount'].name == 'amount'
def test_as_form_fields(self):
class ExampleSerializer(serializers.Serializer):
bool_field = serializers.BooleanField()
null_field = serializers.IntegerField(allow_null=True)
serializer = ExampleSerializer(data={'bool_field': False, 'null_field': None})
assert serializer.is_valid()
assert serializer['bool_field'].as_form_field().value == ''
assert serializer['null_field'].as_form_field().value == ''
class TestNestedBoundField:
def test_nested_empty_bound_field(self):
@ -67,3 +77,16 @@ class TestNestedBoundField:
assert serializer['nested']['amount'].value is None
assert serializer['nested']['amount'].errors is None
assert serializer['nested']['amount'].name == 'nested.amount'
def test_as_form_fields(self):
class Nested(serializers.Serializer):
bool_field = serializers.BooleanField()
null_field = serializers.IntegerField(allow_null=True)
class ExampleSerializer(serializers.Serializer):
nested = Nested()
serializer = ExampleSerializer(data={'nested': {'bool_field': False, 'null_field': None}})
assert serializer.is_valid()
assert serializer['nested']['bool_field'].as_form_field().value == ''
assert serializer['nested']['null_field'].as_form_field().value == ''