mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Make BrowsableAPIRenderer use serializer_class when present
This commit is contained in:
parent
d0126b7e31
commit
77c44d8c63
|
@ -421,6 +421,14 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
return False # Doesn't have permissions
|
return False # Doesn't have permissions
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _get_serializer(self, serializer_class, view_instance, request, *args, **kwargs):
|
||||||
|
kwargs['context'] = {
|
||||||
|
'request': request,
|
||||||
|
'format': self.format,
|
||||||
|
'view': view_instance
|
||||||
|
}
|
||||||
|
return serializer_class(*args, **kwargs)
|
||||||
|
|
||||||
def get_rendered_html_form(self, data, view, method, request):
|
def get_rendered_html_form(self, data, view, method, request):
|
||||||
"""
|
"""
|
||||||
Return a string representing a rendered HTML form, possibly bound to
|
Return a string representing a rendered HTML form, possibly bound to
|
||||||
|
@ -457,8 +465,11 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
if method in ('DELETE', 'OPTIONS'):
|
if method in ('DELETE', 'OPTIONS'):
|
||||||
return True # Don't actually need to return a form
|
return True # Don't actually need to return a form
|
||||||
|
|
||||||
|
has_serializer = getattr(view, 'get_serializer', None)
|
||||||
|
has_serializer_class = getattr(view, 'serializer_class', None)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not getattr(view, 'get_serializer', None) or
|
(not has_serializer and not has_serializer_class) or
|
||||||
not any(is_form_media_type(parser.media_type) for parser in view.parser_classes)
|
not any(is_form_media_type(parser.media_type) for parser in view.parser_classes)
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
@ -466,10 +477,19 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
if existing_serializer is not None:
|
if existing_serializer is not None:
|
||||||
serializer = existing_serializer
|
serializer = existing_serializer
|
||||||
else:
|
else:
|
||||||
|
if has_serializer:
|
||||||
if method in ('PUT', 'PATCH'):
|
if method in ('PUT', 'PATCH'):
|
||||||
serializer = view.get_serializer(instance=instance, **kwargs)
|
serializer = view.get_serializer(instance=instance, **kwargs)
|
||||||
else:
|
else:
|
||||||
serializer = view.get_serializer(**kwargs)
|
serializer = view.get_serializer(**kwargs)
|
||||||
|
else:
|
||||||
|
# at this point we must have a serializer_class
|
||||||
|
if method in ('PUT', 'PATCH'):
|
||||||
|
serializer = self._get_serializer(view.serializer_class, view,
|
||||||
|
request, instance=instance, **kwargs)
|
||||||
|
else:
|
||||||
|
serializer = self._get_serializer(view.serializer_class, view,
|
||||||
|
request, **kwargs)
|
||||||
|
|
||||||
if hasattr(serializer, 'initial_data'):
|
if hasattr(serializer, 'initial_data'):
|
||||||
serializer.is_valid()
|
serializer.is_valid()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user