diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index c635505a4..23387dffb 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -12,7 +12,7 @@ from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser from django.http.multipartparser import MultiPartParserError, parse_header, ChunkIter from rest_framework.compat import etree, six, yaml from rest_framework.exceptions import ParseError -from rest_framework.renderers import UnicodeJSONRenderer +from rest_framework import renderers import json import datetime import decimal @@ -32,8 +32,6 @@ class BaseParser(object): media_type = None - supports_html_forms = False - def parse(self, stream, media_type=None, parser_context=None): """ Given a stream to read from, return the parsed representation. @@ -49,7 +47,7 @@ class JSONParser(BaseParser): """ media_type = 'application/json' - renderer_class = UnicodeJSONRenderer + renderer_class = renderers.UnicodeJSONRenderer def parse(self, stream, media_type=None, parser_context=None): """ @@ -94,7 +92,6 @@ class FormParser(BaseParser): """ media_type = 'application/x-www-form-urlencoded' - supports_html_forms = True def parse(self, stream, media_type=None, parser_context=None): """ @@ -113,7 +110,6 @@ class MultiPartParser(BaseParser): """ media_type = 'multipart/form-data' - supports_html_forms = True def parse(self, stream, media_type=None, parser_context=None): """ @@ -134,7 +130,7 @@ class MultiPartParser(BaseParser): data, files = parser.parse() return DataAndFiles(data, files) except MultiPartParserError as exc: - raise ParseError('Multipart form parse error - %s' % six.u(exc)) + raise ParseError('Multipart form parse error - %s' % six.u(exc.strerror)) class XMLParser(BaseParser): diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index cc8de9590..cd55c7830 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -21,7 +21,7 @@ from rest_framework.compat import six from rest_framework.compat import smart_text from rest_framework.compat import yaml from rest_framework.settings import api_settings -from rest_framework.request import clone_request +from rest_framework.request import clone_request, is_form_media_type from rest_framework.utils import encoders from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework import exceptions, status, VERSION @@ -482,7 +482,7 @@ class BrowsableAPIRenderer(BaseRenderer): if method in ('DELETE', 'OPTIONS'): return True # Don't actually need to return a form - if not getattr(view, 'get_serializer', None) or not any(parser.supports_html_forms for parser in view.parser_classes): + if not getattr(view, 'get_serializer', None) or not any(is_form_media_type(parser.media_type) for parser in view.parser_classes): return serializer = view.get_serializer(instance=obj) @@ -565,11 +565,16 @@ class BrowsableAPIRenderer(BaseRenderer): obj = getattr(view, 'object', None) if getattr(view, 'get_serializer', None): serializer = view.get_serializer(instance=obj) + for field_name, field in serializer.fields.items(): + if field.read_only: + del serializer.fields[field_name] else: serializer = None parsers = [] for parser_class in view.parser_classes: + if is_form_media_type(parser_class.media_type): + continue content = None renderer_class = getattr(parser_class, 'renderer_class', None) if renderer_class and serializer: @@ -650,3 +655,4 @@ class MultiPartRenderer(BaseRenderer): def render(self, data, accepted_media_type=None, renderer_context=None): return encode_multipart(self.BOUNDARY, data) + diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 202d3a096..abff68983 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -304,8 +304,6 @@ class BaseSerializer(WritableField): ret.empty = obj is None for field_name, field in self.fields.items(): - if obj is None and field.read_only: - continue field.initialize(parent=self, field_name=field_name) key = self.get_field_key(field_name) value = field.field_to_native(obj, field_name) diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index 7c2a276ed..c24976603 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -158,7 +158,7 @@ class BasicTests(TestCase): 'email': '', 'content': '', 'created': None, - #'sub_comment': '' + 'sub_comment': '' } self.assertEqual(serializer.data, expected)