Skip to main content

Extensible auto django rest framework api generator

Project description

# Django REST framework auto docs
### What is it
DRF Auto Docs is an extension of [drf-docs](https://github.com/manosim/django-rest-framework-docs).
In addition to [drf-docs](https://github.com/manosim/django-rest-framework-docs) features provides:

* optional response fields(if input is different from output)
* functional view input/output documentation
* tree-like structure
* preserves formatting(spaces & new lines) in docstrings
* markdown in docstrings
* choice field options rendering
* fields' help_text (to specify SerializerMethodField output, for example)
* read_only/required rendering
* possibility to expand docstrings while keeping them short(docstring formatting)

### What isn't supported yet:

* viewsets
* possibility to try in browser
* permission listing
* auth classes
* content types

### Why use this?

* keeps project and documentation synchronized without any effort
* fills those nasty gaps, that made django-rest-swagger/drf-docs unusable in practice



# Samples

### Whole structure:

![whole structure](http://joxi.net/LmGnYqhelBEWrl.jpg)

### Single node:

![single node](http://joxi.net/E2ppYWh9GvEW2Y.jpg)

### Choices:

![choices](http://joxi.net/12M5L7CMkgyb2J.jpg)

### Nested items:

![nested items](http://joxi.net/brRK3EhJOBZdm1.jpg)

### Help text:

![help text](http://joxi.net/n2YXyRsoekWNm6.jpg)

### Docstring formatting:

```python
@format_docstring(request_example, response_example=response_example)
class BookReadUpdateHandler(RetrieveUpdateAPIView):
"""
Wow, this magic decorator allows us to:
1) Keep clean & short docstring
2) Insert additional data in it, like request/response examples

Request: {}
Response: {response_example}
"""
serializer_class = BookUpdateSerializer
```

### Produces:

![help text](http://joxi.net/1A5GqQTnbkbRmE.jpg)


# Installation
In virtualenv:

pip install drf_autodocs

In settings:

INSTALLED_APPS = [
...
'drf_autodocs',
...
]

In your urls:

urlpatterns = [
...
url(r'^', include('drf_autodocs.urls')),
]


That's already enough for swagger-like docs,
result available on

`localhost:8000/docs/`

If you want functional views support and some more features, read below.

# Usage

### Class-Based views
Say you have a view like this:
```python
class BookReadUpdateHandler(RetrieveUpdateAPIView):
serializer_class = BookUpdateSerializer
queryset = Book.objects.all()
```

And say this serializers' input is different from output:
```python
class BookUpdateSerializer(serializers.ModelSerializer):
class Meta:
fields = ('name', 'author')
model = Book

def to_representation(self, instance):
return LibrarySerializer(instance.library)
```

Now to know what return format is, one must make a request.
This package simplifies it via:

`response_serializer_class = YourSerializer`

Now your view looks like:
```python
class BookReadUpdateHandler(RetrieveUpdateAPIView):
"""
Shiny and nice docstring, which:
1) allows formatting
2) `allows markdown`
"""
serializer_class = BookUpdateSerializer
response_serializer_class = LibrarySerializer
queryset = Book.objects.all()
```

### Function-based views

For functional views, decorate them with.

`drf_autodocs.decorators.document_serializer_classes`

Note that response_serializer_class is optional.

Now it should look like
```python
from drf_autodocs.decorators import document_serializer_classes

@document_serializer_classes(serializer_class=BookSerializer, response_serializer_class=LibrarySerializer)
@api_view(['GET', 'POST', 'DELETE'])
def hello_world(request):
"""
Works for `functional` views too!
Yeah, that thing rocks!
"""
return Response('hello_world response')
```

### Docstring formatting

```python
from .request_response_examples import request_example, response_example
from drf_autodocs.decorators import format_docstring


@format_docstring(request_example, response_example=response_example)
class BookReadUpdateHandler(RetrieveUpdateAPIView):
"""
Wow, this magic decorator allows us to:
1) Keep clean & short docstring
2) Insert additional data in it, like request/response examples

Request: {}
Response: {response_example}
"""
serializer_class = BookUpdateSerializer
response_serializer_class = LibrarySerializer
queryset = Book.objects.all()
```


# Customization
To change application look & feel, override

`templates/drf_autodocs/index.html`


For additional parsers(if you want other structure than tree), inherit from

`drf_autodocs.parser.BaseAPIParser`



### Supports
- Python 3(Not tested on 2, though might work)
- Django 1.8+
- Django Rest Framework 3+


# Credits
Thanks to [django](http://djangoproject.com), [django-REST](http://www.django-rest-framework.org/) for their awesome work,
and [drf-docs](https://github.com/manosim/django-rest-framework-docs) for source of inspiration as well as some parts of code

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_autodocs-0.2.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

drf_autodocs-0.2.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file drf_autodocs-0.2.1.tar.gz.

File metadata

File hashes

Hashes for drf_autodocs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 207bbf2b5152a1bfbe1323f1b4cf7a333ca821000acf9fe5514423bcf3199b7c
MD5 56d57b826e96261242f93cfbcc6a101c
BLAKE2b-256 fdc221b118496cfbfc486587d8a08644f85bf38105c15bf6ef1a7d70112e16ba

See more details on using hashes here.

File details

Details for the file drf_autodocs-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_autodocs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7c280d90087c2c7c1e02cac5fc920346a233480e110b5c218b3f9b2c46e403c
MD5 5477f28cb984a4b756d1ba7ccec27143
BLAKE2b-256 bb7598f6e94d6a09de4b337a8a7b7744ea05310531a44a0ef9fc3c116c590069

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page