REST Framework YAML

YAML support for Django REST Framework


Overview

YAML support extracted as a third party package directly from the official Django REST Framework implementation. It's built using the PyYAML package.

Requirements

  • Python (2.7, 3.3, 3.4)
  • Django (1.6, 1.7)

Installation

Install using pip...

$ pip install djangorestframework-yaml 

Example

REST_FRAMEWORK = { 'DEFAULT_PARSER_CLASSES': ( 'rest_framework_yaml.parsers.YAMLParser', ), 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework_yaml.renderers.YAMLRenderer', ), } 

You can also set the renderer and parser used for an individual view, or viewset, using the APIView class based views.

from rest_framework.response import Response from rest_framework.views import APIView from rest_framework_yaml.parsers import YAMLParser from rest_framework_yaml.renderers import YAMLRenderer class ExampleView(APIView): """ A view that can accept POST requests with YAML content. """ parser_classes = (YAMLParser,) renderer_classes = (YAMLRenderer,) def post(self, request, format=None): return Response({'received data': request.DATA}) 

Sample output

--- - email: jpadilla@example.com is_staff: true url: "http://127.0.0.1:8000/users/1/" username: jpadilla 

Testing

Install testing requirements.

$ pip install -r requirements.txt 

Run with runtests.

$ ./runtests.py 

You can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:

$ tox 

Documentation

To build the documentation, you'll need to install mkdocs.

$ pip install mkdocs 

To preview the documentation:

$ mkdocs serve Running at: http://127.0.0.1:8000/ 

To build the documentation:

$ mkdocs build