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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
# 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][original].
## Requirements
* Python (3.8, 3.9, 3.10, 3.11)
* Django (3.2, 4.*)
## Installation
Install using `pip`...
```bash
$ pip install rest_framework_yaml
```
## Example
```python
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.
```python
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
```yaml
---
-
email: jpadilla@example.com
is_staff: true
url: "http://127.0.0.1:8000/users/1/"
username: jpadilla
```
## Testing
Install testing requirements.
```bash
$ poetry install
```
Run with pytest.
```bash
$ poetry run pytest
```
You can also use the excellent [tox](http://tox.readthedocs.org/en/latest/) testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:
```bash
$ tox
```
## Documentation
To build the documentation, you'll need to install `mkdocs`.
```bash
$ poetry install --only docs
```
To preview the documentation:
```bash
$ poetry run mkdocs serve
Running at: http://127.0.0.1:8000/
```
To build the documentation:
```
$ poetry run mkdocs build
```
[original]: https://github.com/jpadilla/django-rest-framework-yaml
|