Django restframework custom schema
Project description
# Django restframework schema
This package is a schema for django restframework.
# How to use
## Installation
```bash
$ pip install django-rest-coreapi-schema
```
```bash
$ pip install -e git+https://github.com/emilioag/django_rest_coreapi_schema.git#egg=django_rest_coreapi_schema
```
## Configuration
### django settings
Add the next configuration in your settings.py file.
```python
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'django_rest_coreapi_schema.schema.CoreAPIAutoSchema',
}
```
### Restframework docs (urls)
Add to your urls.py the restframework docs
```python
from django.conf.urls import url
from rest_framework.documentation import include_docs_urls
urlpatterns = [
url(r'^docs/', include_docs_urls(title='My API title', description='API description', public=False)),
]
```
### Create your serializers
```python
from rest_framework import serializers
class UserSerializer(serializers.Serializer):
email = serializers.CharField(
required=False,
help_text="User email")
address = serializers.CharField(
required=False,
help_text="User address")
class FilterSerializer(serializers.Serializer):
order = serializers.ChoiceField(
required=False,
choices=[("asc", "Asc"), ("desc", "desc")],
help_text="Order")
username = serializers.CharField(
required=False,
help_text="Username pattern")
class PathSerializer(serializers.Serializer):
username = serializers.CharField(
required=True,
help_text="Username")
```
### Create your pagination
```python
from rest_framework.pagination import PageNumberPagination
class LargeResultsSetPagination(PageNumberPagination):
page_size = 1000
page_size_query_param = 'page_size'
max_page_size = 10000
```
## Create your view
### Documenting path variables
You have to use the class variable: `queryset`
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserView(DocumentedBaseView):
queryset = PathSerializer
```
### Url args
You have to use the class variables: `filter_backends` and `filter_fields`
* **filter_backends** is a list of serializers which contains all the possible url args.
* **filter_fields** is a list of arg names that will be appear in the coreapi documentation.
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserListView(DocumentedBaseView):
filter_backends = [FilterSerializer]
filter_fields = ('order', 'username')
```
### Body
Http put, post, etc. body.
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserView(DocumentedBaseView):
body_serializer_class = UserSerializer
```
### Pagination (for large results)
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserListView(DocumentedBaseView):
pagination_class = LargeResultsSetPagination
```
You can see a whole example in examples/restAPI folder inside this repository.
This package is a schema for django restframework.
# How to use
## Installation
```bash
$ pip install django-rest-coreapi-schema
```
```bash
$ pip install -e git+https://github.com/emilioag/django_rest_coreapi_schema.git#egg=django_rest_coreapi_schema
```
## Configuration
### django settings
Add the next configuration in your settings.py file.
```python
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'django_rest_coreapi_schema.schema.CoreAPIAutoSchema',
}
```
### Restframework docs (urls)
Add to your urls.py the restframework docs
```python
from django.conf.urls import url
from rest_framework.documentation import include_docs_urls
urlpatterns = [
url(r'^docs/', include_docs_urls(title='My API title', description='API description', public=False)),
]
```
### Create your serializers
```python
from rest_framework import serializers
class UserSerializer(serializers.Serializer):
email = serializers.CharField(
required=False,
help_text="User email")
address = serializers.CharField(
required=False,
help_text="User address")
class FilterSerializer(serializers.Serializer):
order = serializers.ChoiceField(
required=False,
choices=[("asc", "Asc"), ("desc", "desc")],
help_text="Order")
username = serializers.CharField(
required=False,
help_text="Username pattern")
class PathSerializer(serializers.Serializer):
username = serializers.CharField(
required=True,
help_text="Username")
```
### Create your pagination
```python
from rest_framework.pagination import PageNumberPagination
class LargeResultsSetPagination(PageNumberPagination):
page_size = 1000
page_size_query_param = 'page_size'
max_page_size = 10000
```
## Create your view
### Documenting path variables
You have to use the class variable: `queryset`
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserView(DocumentedBaseView):
queryset = PathSerializer
```
### Url args
You have to use the class variables: `filter_backends` and `filter_fields`
* **filter_backends** is a list of serializers which contains all the possible url args.
* **filter_fields** is a list of arg names that will be appear in the coreapi documentation.
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserListView(DocumentedBaseView):
filter_backends = [FilterSerializer]
filter_fields = ('order', 'username')
```
### Body
Http put, post, etc. body.
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserView(DocumentedBaseView):
body_serializer_class = UserSerializer
```
### Pagination (for large results)
```python
from django_rest_coreapi_schema.views import DocumentedBaseView
class UserListView(DocumentedBaseView):
pagination_class = LargeResultsSetPagination
```
You can see a whole example in examples/restAPI folder inside this repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_rest_coreapi_schema-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | affd1174b6149c60db72f0bde93ce65f07579782716ab9284369d6b426b85759 |
|
MD5 | 7bee0a7d5fdcd44dc8f387f3391be073 |
|
BLAKE2b-256 | c5845a3020fa1477e3435e0ce8c33985db63192da4b35f3ca5d982f00b8c298d |
Close
Hashes for django_rest_coreapi_schema-0.1.1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee3ab944cf45d806a737d5d3467f5b206ef2caf48b93adde1c65334f11dc79f7 |
|
MD5 | f3fc14031e677c5a0f1634d935096b95 |
|
BLAKE2b-256 | 280fc29806b0f67d7699ceccde62110f0225571fc72e7c6abedeca08c1cff869 |