Skip to content

REST Framework YAML

YAML support for Django REST Framework


Overview

YAML support for the Django REST Framework, forked from https://github.com/jpadilla/django-rest-framework-yaml.

Requirements

  • Python (3.8, 3.9, 3.10, 3.11)
  • Django (3.2, 4.*)

Installation

Install using pip...

$ pip install drf-yaml

Example

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'drf_yaml.parsers.YAMLParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'drf_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 drf_yaml.parsers import YAMLParser
from drf_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.

$ poetry install

Run with pytest.

$ poetry run pytest

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.

$ poetry install --only docs

To preview the documentation:

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

To build the documentation:

$ poetry run mkdocs build