8000 support django 2.1 test client json data automatically serialized · encode/django-rest-framework@4607f28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4607f28

Browse files
committed
support django 2.1 test client json data automatically serialized
1 parent d2d1888 commit 4607f28

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

rest_framework/test.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,19 @@ def _encode_data(self, data, format=None, content_type=None):
156156
Encode the data returning a two tuple of (bytes, content_type)
157157
"""
158158

159-
if data is None:
160-
return ('', content_type)
161-
162159
assert format is None or content_type is None, (
163160
'You may not set both `format` and `content_type`.'
164161
)
165162

166163
if content_type:
164+
try:
165+
data = self._encode_json(data, content_type)
166+
except AttributeError:
167+
pass
168+
169+
if data is None:
170+
data = ''
171+
167172
# Content type specified explicitly, treat data as a raw bytestring
168173
ret = force_bytes(data, settings.DEFAULT_CHARSET)
169174

@@ -181,13 +186,17 @@ def _encode_data(self, data, format=None, content_type=None):
181186

182187
# Use format and render the data into a bytestring
183188
renderer = self.renderer_classes[format]()
184-
ret = renderer.render(data)
185189

186190
# Determine the content-type header from the renderer
187191
content_type = "{0}; charset={1}".format(
188192
renderer.media_type, renderer.charset
189193
)
190194

195+
if data is None:
196+
ret = ''
197+
else:
198+
ret = renderer.render(data)
199+
191200
# Coerce text to bytes if required.
192201
if isinstance(ret, six.text_type):
193202
ret = bytes(ret.encode(renderer.charset))

0 commit comments

Comments
 (0)
0