@@ -26,26 +26,26 @@ As an example, if you are sending `json` encoded data using jQuery with the [.aj
...
@@ -26,26 +26,26 @@ As an example, if you are sending `json` encoded data using jQuery with the [.aj
## Setting the parsers
## Setting the parsers
The default set of parsers may be set globally, using the `DEFAULT_PARSER_CLASSES` setting. For example, the following settings would allow requests with `YAML` content.
The default set of parsers may be set globally, using the `DEFAULT_PARSER_CLASSES` setting. For example, the following settings would allow requests with `JSON` content.
REST_FRAMEWORK = {
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': (
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.YAMLParser',
'rest_framework.parsers.JSONParser',
)
)
}
}
You can also set the parsers used for an individual view, or viewset,
You can also set the parsers used for an individual view, or viewset,
using the `APIView` class based views.
using the `APIView` class based views.
from rest_framework.parsers import YAMLParser
from rest_framework.parsers import JSONParser
from rest_framework.response import Response
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.views import APIView
class ExampleView(APIView):
class ExampleView(APIView):
"""
"""
A view that can accept POST requests with YAML content.
A view that can accept POST requests with JSON content.
"""
"""
parser_classes = (YAMLParser,)
parser_classes = (JSONParser,)
def post(self, request, format=None):
def post(self, request, format=None):
return Response({'received data': request.data})
return Response({'received data': request.data})
...
@@ -53,10 +53,10 @@ using the `APIView` class based views.
...
@@ -53,10 +53,10 @@ using the `APIView` class based views.
Or, if you're using the `@api_view` decorator with function based views.
Or, if you're using the `@api_view` decorator with function based views.
@api_view(['POST'])
@api_view(['POST'])
@parser_classes((YAMLParser,))
@parser_classes((JSONParser,))
def example_view(request, format=None):
def example_view(request, format=None):
"""
"""
A view that can accept POST requests with YAML content.
A view that can accept POST requests with JSON content.
"""
"""
return Response({'received data': request.data})
return Response({'received data': request.data})
...
@@ -70,14 +70,6 @@ Parses `JSON` request content.
...
@@ -70,14 +70,6 @@ Parses `JSON` request content.
**.media_type**:`application/json`
**.media_type**:`application/json`
## YAMLParser
Parses `YAML` request content.
Requires the `pyyaml` package to be installed.
**.media_type**:`application/yaml`
## FormParser
## FormParser
Parses HTML form content. `request.data` will be populated with a `QueryDict` of data.
Parses HTML form content. `request.data` will be populated with a `QueryDict` of data.
@@ -18,11 +18,11 @@ For more information see the documentation on [content negotiation][conneg].
...
@@ -18,11 +18,11 @@ For more information see the documentation on [content negotiation][conneg].
## Setting the renderers
## Setting the renderers
The default set of renderers may be set globally, using the `DEFAULT_RENDERER_CLASSES` setting. For example, the following settings would use `YAML` as the main media type and also include the self describing API.
The default set of renderers may be set globally, using the `DEFAULT_RENDERER_CLASSES` setting. For example, the following settings would use `JSON` as the main media type and also include the self describing API.
REST_FRAMEWORK = {
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.YAMLRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
)
}
}
...
@@ -31,15 +31,15 @@ You can also set the renderers used for an individual view, or viewset,
...
@@ -31,15 +31,15 @@ You can also set the renderers used for an individual view, or viewset,
using the `APIView` class based views.
using the `APIView` class based views.
from django.contrib.auth.models import User
from django.contrib.auth.models import User
from rest_framework.renderers import JSONRenderer, YAMLRenderer
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.views import APIView
class UserCountView(APIView):
class UserCountView(APIView):
"""
"""
A view that returns the count of active users, in JSON or YAML.
A view that returns the count of active users in JSON.
@@ -255,14 +255,14 @@ The default format used to make test requests may be set using the `TEST_REQUEST
...
@@ -255,14 +255,14 @@ The default format used to make test requests may be set using the `TEST_REQUEST
If you need to test requests using something other than multipart or json requests, you can do so by setting the `TEST_REQUEST_RENDERER_CLASSES` setting.
If you need to test requests using something other than multipart or json requests, you can do so by setting the `TEST_REQUEST_RENDERER_CLASSES` setting.
For example, to add support for using `format='yaml'` in test requests, you might have something like this in your `settings.py` file.
For example, to add support for using `format='html'` in test requests, you might have something like this in your `settings.py` file.
@@ -92,7 +92,7 @@ Here is the view for an individual snippet, in the `views.py` module.
...
@@ -92,7 +92,7 @@ Here is the view for an individual snippet, in the `views.py` module.
This should all feel very familiar - it is not a lot different from working with regular Django views.
This should all feel very familiar - it is not a lot different from working with regular Django views.
Notice that we're no longer explicitly tying our requests or responses to a given content type. `request.data` can handle incoming `json` requests, but it can also handle `yaml` and other formats. Similarly we're returning response objects with data, but allowing REST framework to render the response into the correct content type for us.
Notice that we're no longer explicitly tying our requests or responses to a given content type. `request.data` can handle incoming `json` requests, but it can also handle other formats. Similarly we're returning response objects with data, but allowing REST framework to render the response into the correct content type for us.