File: renderers.md

package info (click to toggle)
python-djangorestframework-yaml 3.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: python: 483; makefile: 11
file content (62 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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.

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

You can also set the renderers used for an individual view, or viewset,
using the `APIView` class based views.

    from django.contrib.auth.models import User
    from rest_framework.response import Response
    from rest_framework.views import APIView
    from rest_framework_yaml.renderers import YAMLRenderer

    class UserCountView(APIView):
        """
        A view that returns the count of active users in YAML.
        """
        renderer_classes = (YAMLRenderer,)

        def get(self, request, format=None):
            user_count = User.objects.filter(active=True).count()
            content = {'user_count': user_count}
            return Response(content)

Or, if you're using the `@api_view` decorator with function based views.

    @api_view(['GET'])
    @renderer_classes((YAMLRenderer,))
    def user_count_view(request, format=None):
        """
        A view that returns the count of active users in YAML.
        """
        user_count = User.objects.filter(active=True).count()
        content = {'user_count': user_count}
        return Response(content)

---

# API Reference

## YAMLRenderer

Renders the request data into `YAML`.

Requires the `pyyaml` package to be installed.

Note that non-ascii characters will be rendered using `\uXXXX` character escape.  For example:

    unicode black star: "\u2605"

**.media_type**: `application/yaml`

**.format**: `'.yaml'`

**.charset**: `utf-8`