mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 01:26:53 +03:00
Handle non-dict values for NestedSerializer during BrowsableAPI rendering.
This commit is contained in:
parent
05512160ab
commit
95ae92ef23
|
@ -1,5 +1,5 @@
|
|||
from collections import OrderedDict
|
||||
from collections.abc import MutableMapping
|
||||
from collections.abc import Mapping, MutableMapping
|
||||
|
||||
from django.utils.encoding import force_str
|
||||
|
||||
|
@ -101,7 +101,7 @@ class NestedBoundField(BoundField):
|
|||
"""
|
||||
|
||||
def __init__(self, field, value, errors, prefix=''):
|
||||
if value is None or value == '':
|
||||
if value is None or value == '' or not isinstance(value, Mapping):
|
||||
value = {}
|
||||
super().__init__(field, value, errors, prefix)
|
||||
|
||||
|
|
|
@ -163,6 +163,33 @@ class TestNestedBoundField:
|
|||
rendered_packed = ''.join(rendered.split())
|
||||
assert rendered_packed == expected_packed
|
||||
|
||||
def test_rendering_nested_fields_with_not_mappable_value(self):
|
||||
from rest_framework.renderers import HTMLFormRenderer
|
||||
|
||||
class Nested(serializers.Serializer):
|
||||
text_field = serializers.CharField()
|
||||
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
nested = Nested()
|
||||
|
||||
serializer = ExampleSerializer(data={'nested': 1})
|
||||
assert not serializer.is_valid()
|
||||
renderer = HTMLFormRenderer()
|
||||
for field in serializer:
|
||||
rendered = renderer.render_field(field, {})
|
||||
expected_packed = (
|
||||
'<fieldset>'
|
||||
'<legend>Nested</legend>'
|
||||
'<divclass="form-group">'
|
||||
'<label>Textfield</label>'
|
||||
'<inputname="nested.text_field"class="form-control"type="text"value="">'
|
||||
'</div>'
|
||||
'</fieldset>'
|
||||
)
|
||||
|
||||
rendered_packed = ''.join(rendered.split())
|
||||
assert rendered_packed == expected_packed
|
||||
|
||||
|
||||
class TestJSONBoundField:
|
||||
def test_as_form_fields(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user