djangorestframework-yaml

raw JSON →
2.0.0 verified Fri May 01 auth: no python

Provides YAML parsing and rendering for Django REST Framework. Current version 2.0.0 supports Python 3.5+, Django 2.2+, and DRF 3.11+. Release cadence is low; last stable release was 2020.

pip install djangorestframework-yaml
error ImportError: No module named 'rest_framework_yaml'
cause Package not installed or wrong import path.
fix
Run pip install djangorestframework-yaml and import from rest_framework_yaml.
error AttributeError: module 'rest_framework_yaml' has no attribute 'YAMLParser'
cause Incorrect import path e.g., `from rest_framework_yaml import YAMLParser`.
fix
Use from rest_framework_yaml.parsers import YAMLParser.
breaking Version 2.0.0 drops Python 2 support. Upgrade if still on Python 2.
fix Upgrade to 2.0.0 and ensure Python 3.5+.
gotcha The install package name is `djangorestframework-yaml`, not `djangorestframework-yaml` (common typo).
fix Install using `pip install djangorestframework-yaml`.

Add YAML support to a DRF view by including the parser and renderer classes.

from rest_framework import routers, serializers, viewsets
from rest_framework_yaml.renderers import YAMLRenderer
from rest_framework_yaml.parsers import YAMLParser

class UserSerializer(serializers.Serializer):
    name = serializers.CharField()

class UserViewSet(viewsets.ViewSet):
    serializer_class = UserSerializer
    renderer_classes = [YAMLRenderer]
    parser_classes = [YAMLParser]

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
# Then include router.urls in urlpatterns