Giter Club home page Giter Club logo

django_rest_coreapi_schema's Introduction

Django restframework schema

This package is a schema for django restframework.

How to use

Installation

$ pip install django-rest-coreapi-schema
$ 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.

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'django_rest_coreapi_schema.schema.CoreAPIAutoSchema',
}

Restframework docs (urls)

Add to your urls.py the restframework docs

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

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

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

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.
from django_rest_coreapi_schema.views import DocumentedBaseView

class UserListView(DocumentedBaseView):
    filter_backends = [FilterSerializer]
    filter_fields = ('order', 'username')

Body

Http put, post, etc. body.

from django_rest_coreapi_schema.views import DocumentedBaseView

class UserView(DocumentedBaseView):
    body_serializer_class = UserSerializer

Pagination (for large results)

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.

django_rest_coreapi_schema's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.