mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Merge pull request #3544 from dursk/nested-as-form-fields
Correctly handle False in NestedBoundField.as_form_field()
This commit is contained in:
commit
fa6f053323
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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 == ''
|
||||
|
|
Loading…
Reference in New Issue
Block a user