mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 18:13:57 +03:00
1241 lines
55 KiB
HTML
1241 lines
55 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<meta charset="utf-8">
|
|
<title>Schemas - Django REST framework</title>
|
|
<link href="../../img/favicon.ico" rel="icon" type="image/x-icon">
|
|
<link rel="canonical" href="https://www.django-rest-framework.org/coreapi/schemas/" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="description" content="Django, API, REST, Schemas">
|
|
<meta name="author" content="Tom Christie">
|
|
|
|
<!-- Le styles -->
|
|
<link href="../../css/prettify.css" rel="stylesheet">
|
|
<link href="../../css/bootstrap.css" rel="stylesheet">
|
|
<link href="../../css/bootstrap-responsive.css" rel="stylesheet">
|
|
<link href="../../css/default.css" rel="stylesheet">
|
|
|
|
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
|
|
<!--[if lt IE 9]>
|
|
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
|
|
<script type="text/javascript">
|
|
var _gaq = _gaq || [];
|
|
_gaq.push(['_setAccount', 'UA-18852272-2']);
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
(function() {
|
|
var ga = document.createElement('script');
|
|
ga.type = 'text/javascript';
|
|
ga.async = true;
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
|
var s = document.getElementsByTagName('script')[0];
|
|
s.parentNode.insertBefore(ga, s);
|
|
})();
|
|
</script>
|
|
|
|
<style>
|
|
#sidebarInclude img {
|
|
margin-bottom: 10px;
|
|
}
|
|
#sidebarInclude a.promo {
|
|
color: black;
|
|
}
|
|
@media (max-width: 767px) {
|
|
div.promo {
|
|
display: none;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body onload="prettyPrint()" class="-page">
|
|
|
|
<div class="wrapper">
|
|
<div class="navbar navbar-inverse navbar-fixed-top">
|
|
<div class="navbar-inner">
|
|
<div class="container-fluid">
|
|
<a class="repo-link btn btn-primary btn-small" href="https://github.com/encode/django-rest-framework/tree/master">GitHub</a>
|
|
<a class="repo-link btn btn-inverse btn-small disabled" rel="next" >
|
|
Next <i class="icon-arrow-right icon-white"></i>
|
|
</a>
|
|
<a class="repo-link btn btn-inverse btn-small disabled" rel="prev" >
|
|
<i class="icon-arrow-left icon-white"></i> Previous
|
|
</a>
|
|
<a id="search_modal_show" class="repo-link btn btn-inverse btn-small" href="#mkdocs_search_modal" data-toggle="modal" data-target="#mkdocs_search_modal"><i class="icon-search icon-white"></i> Search</a>
|
|
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</a>
|
|
<a class="brand" href="https://www.django-rest-framework.org/">Django REST framework</a>
|
|
<div class="nav-collapse collapse">
|
|
|
|
<!-- Main navigation -->
|
|
<ul class="nav navbar-nav">
|
|
|
|
<li >
|
|
<a href="../..">Home</a>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Tutorial <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../tutorial/quickstart/">Quickstart</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/1-serialization/">1 - Serialization</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/2-requests-and-responses/">2 - Requests and responses</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/3-class-based-views/">3 - Class based views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/4-authentication-and-permissions/">4 - Authentication and permissions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/5-relationships-and-hyperlinked-apis/">5 - Relationships and hyperlinked APIs</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../tutorial/6-viewsets-and-routers/">6 - Viewsets and routers</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API Guide <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../api-guide/requests/">Requests</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/responses/">Responses</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/views/">Views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/generic-views/">Generic views</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/viewsets/">Viewsets</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/routers/">Routers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/parsers/">Parsers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/renderers/">Renderers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/serializers/">Serializers</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/fields/">Serializer fields</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/relations/">Serializer relations</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/validators/">Validators</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/authentication/">Authentication</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/permissions/">Permissions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/caching/">Caching</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/throttling/">Throttling</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/filtering/">Filtering</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/pagination/">Pagination</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/versioning/">Versioning</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/content-negotiation/">Content negotiation</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/metadata/">Metadata</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/schemas/">Schemas</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/format-suffixes/">Format suffixes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/reverse/">Returning URLs</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/exceptions/">Exceptions</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/status-codes/">Status codes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/testing/">Testing</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../api-guide/settings/">Settings</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topics <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../topics/documenting-your-api/">Documenting your API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/api-clients/">API Clients</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/internationalization/">Internationalization</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/ajax-csrf-cors/">AJAX, CSRF & CORS</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/html-and-forms/">HTML & Forms</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/browser-enhancements/">Browser Enhancements</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/browsable-api/">The Browsable API</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../topics/rest-hypermedia-hateoas/">REST, Hypermedia & HATEOAS</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Community <b class="caret"></b></a>
|
|
<ul class="dropdown-menu">
|
|
|
|
<li >
|
|
<a href="../../community/tutorials-and-resources/">Tutorials and Resources</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/third-party-packages/">Third Party Packages</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/contributing/">Contributing to REST framework</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/project-management/">Project management</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/release-notes/">Release Notes</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.10-announcement/">3.10 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.9-announcement/">3.9 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.8-announcement/">3.8 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.7-announcement/">3.7 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.6-announcement/">3.6 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.5-announcement/">3.5 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.4-announcement/">3.4 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.3-announcement/">3.3 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.2-announcement/">3.2 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.1-announcement/">3.1 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/3.0-announcement/">3.0 Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/kickstarter-announcement/">Kickstarter Announcement</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/mozilla-grant/">Mozilla Grant</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/funding/">Funding</a>
|
|
</li>
|
|
|
|
<li >
|
|
<a href="../../community/jobs/">Jobs</a>
|
|
</li>
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
<!--/.nav-collapse -->
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="body-content">
|
|
<div class="container-fluid">
|
|
<!-- Search Modal -->
|
|
<div id="mkdocs_search_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
|
|
<div class="modal-header">
|
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
|
<h3 id="myModalLabel">Documentation search</h3>
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
<form role="form" autocomplete="off">
|
|
<div class="form-group">
|
|
<input type="text" name="q" class="form-control" placeholder="Search..." id="mkdocs-search-query">
|
|
</div>
|
|
</form>
|
|
<div id="mkdocs-search-results"></div>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row-fluid">
|
|
<div class="span3">
|
|
<div id="table-of-contents">
|
|
<ul class="nav nav-list side-nav well sidebar-nav-fixed">
|
|
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#schemas">Schemas</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#install-core-api-pyyaml">Install Core API & PyYAML</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#quickstart">Quickstart</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#internal-schema-representation">Internal schema representation</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#schema-output-formats">Schema output formats</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#schemas-vs-hypermedia">Schemas vs Hypermedia</a>
|
|
</li>
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#creating-a-schema">Creating a schema</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#manual-schema-specification">Manual Schema Specification</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#automatic-schema-generation">Automatic Schema Generation</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#per-view-schema-customisation">Per-View Schema Customisation</a>
|
|
</li>
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#adding-a-schema-view">Adding a schema view</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#the-get_schema_view-shortcut">The get_schema_view shortcut</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#using-an-explicit-schema-view">Using an explicit schema view</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#explicit-schema-definition">Explicit schema definition</a>
|
|
</li>
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#schemas-as-documentation">Schemas as documentation</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#examples">Examples</a>
|
|
</li>
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#api-reference">API Reference</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#schemagenerator">SchemaGenerator</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#autoschema">AutoSchema</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#manualschema">ManualSchema</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="../../#core-api">Core API</a>
|
|
</li>
|
|
|
|
|
|
<li class="main">
|
|
<a href="../../#third-party-packages">Third party packages</a>
|
|
</li>
|
|
|
|
|
|
<li>
|
|
<a href="../../#drf-yasg-yet-another-swagger-generator">drf-yasg - Yet Another Swagger Generator</a>
|
|
</li>
|
|
|
|
|
|
|
|
<div class="promo">
|
|
<hr/>
|
|
<div id="sidebarInclude">
|
|
</div>
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="main-content" class="span9">
|
|
|
|
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/s">
|
|
<span class="label label-info">s</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/c">
|
|
<span class="label label-info">c</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/h">
|
|
<span class="label label-info">h</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/e">
|
|
<span class="label label-info">e</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/m">
|
|
<span class="label label-info">m</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/a">
|
|
<span class="label label-info">a</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/s">
|
|
<span class="label label-info">s</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/.">
|
|
<span class="label label-info">.</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/p">
|
|
<span class="label label-info">p</span>
|
|
</a>
|
|
|
|
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/y">
|
|
<span class="label label-info">y</span>
|
|
</a>
|
|
|
|
|
|
|
|
<h1 id="schemas"><a class="toclink" href="#schemas">Schemas</a></h1>
|
|
<blockquote>
|
|
<p>A machine-readable [schema] describes what resources are available via the API, what their URLs are, how they are represented and what operations they support.</p>
|
|
<p>— Heroku, <a href="https://blog.heroku.com/archives/2014/1/8/json_schema_for_heroku_platform_api">JSON Schema for the Heroku Platform API</a></p>
|
|
</blockquote>
|
|
<p>API schemas are a useful tool that allow for a range of use cases, including
|
|
generating reference documentation, or driving dynamic client libraries that
|
|
can interact with your API.</p>
|
|
<h2 id="install-core-api-pyyaml"><a class="toclink" href="#install-core-api-pyyaml">Install Core API & PyYAML</a></h2>
|
|
<p>You'll need to install the <code>coreapi</code> package in order to add schema support
|
|
for REST framework. You probably also want to install <code>pyyaml</code>, so that you
|
|
can render the schema into the commonly used YAML-based OpenAPI format.</p>
|
|
<pre><code>pip install coreapi pyyaml
|
|
</code></pre>
|
|
<h2 id="quickstart"><a class="toclink" href="#quickstart">Quickstart</a></h2>
|
|
<p>There are two different ways you can serve a schema description for your API.</p>
|
|
<h3 id="generating-a-schema-with-the-generateschema-management-command"><a class="toclink" href="#generating-a-schema-with-the-generateschema-management-command">Generating a schema with the <code>generateschema</code> management command</a></h3>
|
|
<p>To generate a static API schema, use the <code>generateschema</code> management command.</p>
|
|
<pre><code class="shell">$ python manage.py generateschema > schema.yml
|
|
</code></pre>
|
|
|
|
<p>Once you've generated a schema in this way you can annotate it with any
|
|
additional information that cannot be automatically inferred by the schema
|
|
generator.</p>
|
|
<p>You might want to check your API schema into version control and update it
|
|
with each new release, or serve the API schema from your site's static media.</p>
|
|
<h3 id="adding-a-view-with-get_schema_view"><a class="toclink" href="#adding-a-view-with-get_schema_view">Adding a view with <code>get_schema_view</code></a></h3>
|
|
<p>To add a dynamically generated schema view to your API, use <code>get_schema_view</code>.</p>
|
|
<pre><code class="python">from rest_framework.schemas import get_schema_view
|
|
|
|
schema_view = get_schema_view(title="Example API")
|
|
|
|
urlpatterns = [
|
|
url('^schema$', schema_view),
|
|
...
|
|
]
|
|
</code></pre>
|
|
|
|
<p>See below <a href="#the-get_schema_view-shortcut">for more details</a> on customizing a
|
|
dynamically generated schema view.</p>
|
|
<h2 id="internal-schema-representation"><a class="toclink" href="#internal-schema-representation">Internal schema representation</a></h2>
|
|
<p>REST framework uses <a href="https://www.coreapi.org/">Core API</a> in order to model schema information in
|
|
a format-independent representation. This information can then be rendered
|
|
into various different schema formats, or used to generate API documentation.</p>
|
|
<p>When using Core API, a schema is represented as a <code>Document</code> which is the
|
|
top-level container object for information about the API. Available API
|
|
interactions are represented using <code>Link</code> objects. Each link includes a URL,
|
|
HTTP method, and may include a list of <code>Field</code> instances, which describe any
|
|
parameters that may be accepted by the API endpoint. The <code>Link</code> and <code>Field</code>
|
|
instances may also include descriptions, that allow an API schema to be
|
|
rendered into user documentation.</p>
|
|
<p>Here's an example of an API description that includes a single <code>search</code>
|
|
endpoint:</p>
|
|
<pre><code>coreapi.Document(
|
|
title='Flight Search API',
|
|
url='https://api.example.org/',
|
|
content={
|
|
'search': coreapi.Link(
|
|
url='/search/',
|
|
action='get',
|
|
fields=[
|
|
coreapi.Field(
|
|
name='from',
|
|
required=True,
|
|
location='query',
|
|
description='City name or airport code.'
|
|
),
|
|
coreapi.Field(
|
|
name='to',
|
|
required=True,
|
|
location='query',
|
|
description='City name or airport code.'
|
|
),
|
|
coreapi.Field(
|
|
name='date',
|
|
required=True,
|
|
location='query',
|
|
description='Flight date in "YYYY-MM-DD" format.'
|
|
)
|
|
],
|
|
description='Return flight availability and prices.'
|
|
)
|
|
}
|
|
)
|
|
</code></pre>
|
|
<h2 id="schema-output-formats"><a class="toclink" href="#schema-output-formats">Schema output formats</a></h2>
|
|
<p>In order to be presented in an HTTP response, the internal representation
|
|
has to be rendered into the actual bytes that are used in the response.</p>
|
|
<p>REST framework includes a few different renderers that you can use for
|
|
encoding the API schema.</p>
|
|
<ul>
|
|
<li><code>renderers.OpenAPIRenderer</code> - Renders into YAML-based <a href="https://openapis.org/">OpenAPI</a>, the most widely used API schema format.</li>
|
|
<li><code>renderers.JSONOpenAPIRenderer</code> - Renders into JSON-based <a href="https://openapis.org/">OpenAPI</a>.</li>
|
|
<li><code>renderers.CoreJSONRenderer</code> - Renders into <a href="https://www.coreapi.org/specification/encoding/#core-json-encoding">Core JSON</a>, a format designed for
|
|
use with the <code>coreapi</code> client library.</li>
|
|
</ul>
|
|
<p><a href="https://www.coreapi.org/specification/encoding/#core-json-encoding">Core JSON</a> is designed as a canonical format for use with Core API.
|
|
REST framework includes a renderer class for handling this media type, which
|
|
is available as <code>renderers.CoreJSONRenderer</code>.</p>
|
|
<h2 id="schemas-vs-hypermedia"><a class="toclink" href="#schemas-vs-hypermedia">Schemas vs Hypermedia</a></h2>
|
|
<p>It's worth pointing out here that Core API can also be used to model hypermedia
|
|
responses, which present an alternative interaction style to API schemas.</p>
|
|
<p>With an API schema, the entire available interface is presented up-front
|
|
as a single endpoint. Responses to individual API endpoints are then typically
|
|
presented as plain data, without any further interactions contained in each
|
|
response.</p>
|
|
<p>With Hypermedia, the client is instead presented with a document containing
|
|
both data and available interactions. Each interaction results in a new
|
|
document, detailing both the current state and the available interactions.</p>
|
|
<p>Further information and support on building Hypermedia APIs with REST framework
|
|
is planned for a future version.</p>
|
|
<hr />
|
|
<h1 id="creating-a-schema"><a class="toclink" href="#creating-a-schema">Creating a schema</a></h1>
|
|
<p>REST framework includes functionality for auto-generating a schema,
|
|
or allows you to specify one explicitly.</p>
|
|
<h2 id="manual-schema-specification"><a class="toclink" href="#manual-schema-specification">Manual Schema Specification</a></h2>
|
|
<p>To manually specify a schema you create a Core API <code>Document</code>, similar to the
|
|
example above.</p>
|
|
<pre><code>schema = coreapi.Document(
|
|
title='Flight Search API',
|
|
content={
|
|
...
|
|
}
|
|
)
|
|
</code></pre>
|
|
<h2 id="automatic-schema-generation"><a class="toclink" href="#automatic-schema-generation">Automatic Schema Generation</a></h2>
|
|
<p>Automatic schema generation is provided by the <code>SchemaGenerator</code> class.</p>
|
|
<p><code>SchemaGenerator</code> processes a list of routed URL patterns and compiles the
|
|
appropriately structured Core API Document.</p>
|
|
<p>Basic usage is just to provide the title for your schema and call
|
|
<code>get_schema()</code>:</p>
|
|
<pre><code>generator = schemas.SchemaGenerator(title='Flight Search API')
|
|
schema = generator.get_schema()
|
|
</code></pre>
|
|
<h2 id="per-view-schema-customisation"><a class="toclink" href="#per-view-schema-customisation">Per-View Schema Customisation</a></h2>
|
|
<p>By default, view introspection is performed by an <code>AutoSchema</code> instance
|
|
accessible via the <code>schema</code> attribute on <code>APIView</code>. This provides the
|
|
appropriate Core API <code>Link</code> object for the view, request method and path:</p>
|
|
<pre><code>auto_schema = view.schema
|
|
coreapi_link = auto_schema.get_link(...)
|
|
</code></pre>
|
|
<p>(In compiling the schema, <code>SchemaGenerator</code> calls <code>view.schema.get_link()</code> for
|
|
each view, allowed method and path.)</p>
|
|
<hr />
|
|
<p><strong>Note</strong>: For basic <code>APIView</code> subclasses, default introspection is essentially
|
|
limited to the URL kwarg path parameters. For <code>GenericAPIView</code>
|
|
subclasses, which includes all the provided class based views, <code>AutoSchema</code> will
|
|
attempt to introspect serialiser, pagination and filter fields, as well as
|
|
provide richer path field descriptions. (The key hooks here are the relevant
|
|
<code>GenericAPIView</code> attributes and methods: <code>get_serializer</code>, <code>pagination_class</code>,
|
|
<code>filter_backends</code> and so on.)</p>
|
|
<hr />
|
|
<p>To customise the <code>Link</code> generation you may:</p>
|
|
<ul>
|
|
<li>
|
|
<p>Instantiate <code>AutoSchema</code> on your view with the <code>manual_fields</code> kwarg:</p>
|
|
<pre><code>from rest_framework.views import APIView
|
|
from rest_framework.schemas import AutoSchema
|
|
|
|
class CustomView(APIView):
|
|
...
|
|
schema = AutoSchema(
|
|
manual_fields=[
|
|
coreapi.Field("extra_field", ...),
|
|
]
|
|
)
|
|
</code></pre>
|
|
<p>This allows extension for the most common case without subclassing.</p>
|
|
</li>
|
|
<li>
|
|
<p>Provide an <code>AutoSchema</code> subclass with more complex customisation:</p>
|
|
<pre><code>from rest_framework.views import APIView
|
|
from rest_framework.schemas import AutoSchema
|
|
|
|
class CustomSchema(AutoSchema):
|
|
def get_link(...):
|
|
# Implement custom introspection here (or in other sub-methods)
|
|
|
|
class CustomView(APIView):
|
|
...
|
|
schema = CustomSchema()
|
|
</code></pre>
|
|
<p>This provides complete control over view introspection.</p>
|
|
</li>
|
|
<li>
|
|
<p>Instantiate <code>ManualSchema</code> on your view, providing the Core API <code>Fields</code> for
|
|
the view explicitly:</p>
|
|
<pre><code>from rest_framework.views import APIView
|
|
from rest_framework.schemas import ManualSchema
|
|
|
|
class CustomView(APIView):
|
|
...
|
|
schema = ManualSchema(fields=[
|
|
coreapi.Field(
|
|
"first_field",
|
|
required=True,
|
|
location="path",
|
|
schema=coreschema.String()
|
|
),
|
|
coreapi.Field(
|
|
"second_field",
|
|
required=True,
|
|
location="path",
|
|
schema=coreschema.String()
|
|
),
|
|
])
|
|
</code></pre>
|
|
<p>This allows manually specifying the schema for some views whilst maintaining
|
|
automatic generation elsewhere.</p>
|
|
</li>
|
|
</ul>
|
|
<p>You may disable schema generation for a view by setting <code>schema</code> to <code>None</code>:</p>
|
|
<pre><code> class CustomView(APIView):
|
|
...
|
|
schema = None # Will not appear in schema
|
|
</code></pre>
|
|
<p>This also applies to extra actions for <code>ViewSet</code>s:</p>
|
|
<pre><code> class CustomViewSet(viewsets.ModelViewSet):
|
|
|
|
@action(detail=True, schema=None)
|
|
def extra_action(self, request, pk=None):
|
|
...
|
|
</code></pre>
|
|
<hr />
|
|
<p><strong>Note</strong>: For full details on <code>SchemaGenerator</code> plus the <code>AutoSchema</code> and
|
|
<code>ManualSchema</code> descriptors see the <a href="#api-reference">API Reference below</a>.</p>
|
|
<hr />
|
|
<h1 id="adding-a-schema-view"><a class="toclink" href="#adding-a-schema-view">Adding a schema view</a></h1>
|
|
<p>There are a few different ways to add a schema view to your API, depending on
|
|
exactly what you need.</p>
|
|
<h2 id="the-get_schema_view-shortcut"><a class="toclink" href="#the-get_schema_view-shortcut">The get_schema_view shortcut</a></h2>
|
|
<p>The simplest way to include a schema in your project is to use the
|
|
<code>get_schema_view()</code> function.</p>
|
|
<pre><code>from rest_framework.schemas import get_schema_view
|
|
|
|
schema_view = get_schema_view(title="Server Monitoring API")
|
|
|
|
urlpatterns = [
|
|
url('^$', schema_view),
|
|
...
|
|
]
|
|
</code></pre>
|
|
<p>Once the view has been added, you'll be able to make API requests to retrieve
|
|
the auto-generated schema definition.</p>
|
|
<pre><code>$ http http://127.0.0.1:8000/ Accept:application/coreapi+json
|
|
HTTP/1.0 200 OK
|
|
Allow: GET, HEAD, OPTIONS
|
|
Content-Type: application/vnd.coreapi+json
|
|
|
|
{
|
|
"_meta": {
|
|
"title": "Server Monitoring API"
|
|
},
|
|
"_type": "document",
|
|
...
|
|
}
|
|
</code></pre>
|
|
<p>The arguments to <code>get_schema_view()</code> are:</p>
|
|
<h4 id="title"><a class="toclink" href="#title"><code>title</code></a></h4>
|
|
<p>May be used to provide a descriptive title for the schema definition.</p>
|
|
<h4 id="url"><a class="toclink" href="#url"><code>url</code></a></h4>
|
|
<p>May be used to pass a canonical URL for the schema.</p>
|
|
<pre><code>schema_view = get_schema_view(
|
|
title='Server Monitoring API',
|
|
url='https://www.example.org/api/'
|
|
)
|
|
</code></pre>
|
|
<h4 id="urlconf"><a class="toclink" href="#urlconf"><code>urlconf</code></a></h4>
|
|
<p>A string representing the import path to the URL conf that you want
|
|
to generate an API schema for. This defaults to the value of Django's
|
|
ROOT_URLCONF setting.</p>
|
|
<pre><code>schema_view = get_schema_view(
|
|
title='Server Monitoring API',
|
|
url='https://www.example.org/api/',
|
|
urlconf='myproject.urls'
|
|
)
|
|
</code></pre>
|
|
<h4 id="renderer_classes"><a class="toclink" href="#renderer_classes"><code>renderer_classes</code></a></h4>
|
|
<p>May be used to pass the set of renderer classes that can be used to render the API root endpoint.</p>
|
|
<pre><code>from rest_framework.schemas import get_schema_view
|
|
from rest_framework.renderers import JSONOpenAPIRenderer
|
|
|
|
schema_view = get_schema_view(
|
|
title='Server Monitoring API',
|
|
url='https://www.example.org/api/',
|
|
renderer_classes=[JSONOpenAPIRenderer]
|
|
)
|
|
</code></pre>
|
|
<h4 id="patterns"><a class="toclink" href="#patterns"><code>patterns</code></a></h4>
|
|
<p>List of url patterns to limit the schema introspection to. If you only want the <code>myproject.api</code> urls
|
|
to be exposed in the schema:</p>
|
|
<pre><code>schema_url_patterns = [
|
|
url(r'^api/', include('myproject.api.urls')),
|
|
]
|
|
|
|
schema_view = get_schema_view(
|
|
title='Server Monitoring API',
|
|
url='https://www.example.org/api/',
|
|
patterns=schema_url_patterns,
|
|
)
|
|
</code></pre>
|
|
<h4 id="generator_class"><a class="toclink" href="#generator_class"><code>generator_class</code></a></h4>
|
|
<p>May be used to specify a <code>SchemaGenerator</code> subclass to be passed to the
|
|
<code>SchemaView</code>.</p>
|
|
<h4 id="authentication_classes"><a class="toclink" href="#authentication_classes"><code>authentication_classes</code></a></h4>
|
|
<p>May be used to specify the list of authentication classes that will apply to the schema endpoint.
|
|
Defaults to <code>settings.DEFAULT_AUTHENTICATION_CLASSES</code></p>
|
|
<h4 id="permission_classes"><a class="toclink" href="#permission_classes"><code>permission_classes</code></a></h4>
|
|
<p>May be used to specify the list of permission classes that will apply to the schema endpoint.
|
|
Defaults to <code>settings.DEFAULT_PERMISSION_CLASSES</code></p>
|
|
<h2 id="using-an-explicit-schema-view"><a class="toclink" href="#using-an-explicit-schema-view">Using an explicit schema view</a></h2>
|
|
<p>If you need a little more control than the <code>get_schema_view()</code> shortcut gives you,
|
|
then you can use the <code>SchemaGenerator</code> class directly to auto-generate the
|
|
<code>Document</code> instance, and to return that from a view.</p>
|
|
<p>This option gives you the flexibility of setting up the schema endpoint
|
|
with whatever behaviour you want. For example, you can apply different
|
|
permission, throttling, or authentication policies to the schema endpoint.</p>
|
|
<p>Here's an example of using <code>SchemaGenerator</code> together with a view to
|
|
return the schema.</p>
|
|
<p><strong>views.py:</strong></p>
|
|
<pre><code>from rest_framework.decorators import api_view, renderer_classes
|
|
from rest_framework import renderers, response, schemas
|
|
|
|
generator = schemas.SchemaGenerator(title='Bookings API')
|
|
|
|
@api_view()
|
|
@renderer_classes([renderers.OpenAPIRenderer])
|
|
def schema_view(request):
|
|
schema = generator.get_schema(request)
|
|
return response.Response(schema)
|
|
</code></pre>
|
|
<p><strong>urls.py:</strong></p>
|
|
<pre><code>urlpatterns = [
|
|
url('/', schema_view),
|
|
...
|
|
]
|
|
</code></pre>
|
|
<p>You can also serve different schemas to different users, depending on the
|
|
permissions they have available. This approach can be used to ensure that
|
|
unauthenticated requests are presented with a different schema to
|
|
authenticated requests, or to ensure that different parts of the API are
|
|
made visible to different users depending on their role.</p>
|
|
<p>In order to present a schema with endpoints filtered by user permissions,
|
|
you need to pass the <code>request</code> argument to the <code>get_schema()</code> method, like so:</p>
|
|
<pre><code>@api_view()
|
|
@renderer_classes([renderers.OpenAPIRenderer])
|
|
def schema_view(request):
|
|
generator = schemas.SchemaGenerator(title='Bookings API')
|
|
return response.Response(generator.get_schema(request=request))
|
|
</code></pre>
|
|
<h2 id="explicit-schema-definition"><a class="toclink" href="#explicit-schema-definition">Explicit schema definition</a></h2>
|
|
<p>An alternative to the auto-generated approach is to specify the API schema
|
|
explicitly, by declaring a <code>Document</code> object in your codebase. Doing so is a
|
|
little more work, but ensures that you have full control over the schema
|
|
representation.</p>
|
|
<pre><code>import coreapi
|
|
from rest_framework.decorators import api_view, renderer_classes
|
|
from rest_framework import renderers, response
|
|
|
|
schema = coreapi.Document(
|
|
title='Bookings API',
|
|
content={
|
|
...
|
|
}
|
|
)
|
|
|
|
@api_view()
|
|
@renderer_classes([renderers.OpenAPIRenderer])
|
|
def schema_view(request):
|
|
return response.Response(schema)
|
|
</code></pre>
|
|
<hr />
|
|
<h1 id="schemas-as-documentation"><a class="toclink" href="#schemas-as-documentation">Schemas as documentation</a></h1>
|
|
<p>One common usage of API schemas is to use them to build documentation pages.</p>
|
|
<p>The schema generation in REST framework uses docstrings to automatically
|
|
populate descriptions in the schema document.</p>
|
|
<p>These descriptions will be based on:</p>
|
|
<ul>
|
|
<li>The corresponding method docstring if one exists.</li>
|
|
<li>A named section within the class docstring, which can be either single line or multi-line.</li>
|
|
<li>The class docstring.</li>
|
|
</ul>
|
|
<h2 id="examples"><a class="toclink" href="#examples">Examples</a></h2>
|
|
<p>An <code>APIView</code>, with an explicit method docstring.</p>
|
|
<pre><code>class ListUsernames(APIView):
|
|
def get(self, request):
|
|
"""
|
|
Return a list of all user names in the system.
|
|
"""
|
|
usernames = [user.username for user in User.objects.all()]
|
|
return Response(usernames)
|
|
</code></pre>
|
|
<p>A <code>ViewSet</code>, with an explict action docstring.</p>
|
|
<pre><code>class ListUsernames(ViewSet):
|
|
def list(self, request):
|
|
"""
|
|
Return a list of all user names in the system.
|
|
"""
|
|
usernames = [user.username for user in User.objects.all()]
|
|
return Response(usernames)
|
|
</code></pre>
|
|
<p>A generic view with sections in the class docstring, using single-line style.</p>
|
|
<pre><code>class UserList(generics.ListCreateAPIView):
|
|
"""
|
|
get: List all the users.
|
|
post: Create a new user.
|
|
"""
|
|
queryset = User.objects.all()
|
|
serializer_class = UserSerializer
|
|
permission_classes = [IsAdminUser]
|
|
</code></pre>
|
|
<p>A generic viewset with sections in the class docstring, using multi-line style.</p>
|
|
<pre><code>class UserViewSet(viewsets.ModelViewSet):
|
|
"""
|
|
API endpoint that allows users to be viewed or edited.
|
|
|
|
retrieve:
|
|
Return a user instance.
|
|
|
|
list:
|
|
Return all users, ordered by most recently joined.
|
|
"""
|
|
queryset = User.objects.all().order_by('-date_joined')
|
|
serializer_class = UserSerializer
|
|
</code></pre>
|
|
<hr />
|
|
<h1 id="api-reference"><a class="toclink" href="#api-reference">API Reference</a></h1>
|
|
<h2 id="schemagenerator"><a class="toclink" href="#schemagenerator">SchemaGenerator</a></h2>
|
|
<p>A class that walks a list of routed URL patterns, requests the schema for each view,
|
|
and collates the resulting CoreAPI Document.</p>
|
|
<p>Typically you'll instantiate <code>SchemaGenerator</code> with a single argument, like so:</p>
|
|
<pre><code>generator = SchemaGenerator(title='Stock Prices API')
|
|
</code></pre>
|
|
<p>Arguments:</p>
|
|
<ul>
|
|
<li><code>title</code> <strong>required</strong> - The name of the API.</li>
|
|
<li><code>url</code> - The root URL of the API schema. This option is not required unless the schema is included under path prefix.</li>
|
|
<li><code>patterns</code> - A list of URLs to inspect when generating the schema. Defaults to the project's URL conf.</li>
|
|
<li><code>urlconf</code> - A URL conf module name to use when generating the schema. Defaults to <code>settings.ROOT_URLCONF</code>.</li>
|
|
</ul>
|
|
<h3 id="get_schemaself-request"><a class="toclink" href="#get_schemaself-request">get_schema(self, request)</a></h3>
|
|
<p>Returns a <code>coreapi.Document</code> instance that represents the API schema.</p>
|
|
<pre><code>@api_view
|
|
@renderer_classes([renderers.OpenAPIRenderer])
|
|
def schema_view(request):
|
|
generator = schemas.SchemaGenerator(title='Bookings API')
|
|
return Response(generator.get_schema())
|
|
</code></pre>
|
|
<p>The <code>request</code> argument is optional, and may be used if you want to apply per-user
|
|
permissions to the resulting schema generation.</p>
|
|
<h3 id="get_linksself-request"><a class="toclink" href="#get_linksself-request">get_links(self, request)</a></h3>
|
|
<p>Return a nested dictionary containing all the links that should be included in the API schema.</p>
|
|
<p>This is a good point to override if you want to modify the resulting structure of the generated schema,
|
|
as you can build a new dictionary with a different layout.</p>
|
|
<h2 id="autoschema"><a class="toclink" href="#autoschema">AutoSchema</a></h2>
|
|
<p>A class that deals with introspection of individual views for schema generation.</p>
|
|
<p><code>AutoSchema</code> is attached to <code>APIView</code> via the <code>schema</code> attribute.</p>
|
|
<p>The <code>AutoSchema</code> constructor takes a single keyword argument <code>manual_fields</code>.</p>
|
|
<p><strong><code>manual_fields</code></strong>: a <code>list</code> of <code>coreapi.Field</code> instances that will be added to
|
|
the generated fields. Generated fields with a matching <code>name</code> will be overwritten.</p>
|
|
<pre><code>class CustomView(APIView):
|
|
schema = AutoSchema(manual_fields=[
|
|
coreapi.Field(
|
|
"my_extra_field",
|
|
required=True,
|
|
location="path",
|
|
schema=coreschema.String()
|
|
),
|
|
])
|
|
</code></pre>
|
|
<p>For more advanced customisation subclass <code>AutoSchema</code> to customise schema generation.</p>
|
|
<pre><code>class CustomViewSchema(AutoSchema):
|
|
"""
|
|
Overrides `get_link()` to provide Custom Behavior X
|
|
"""
|
|
|
|
def get_link(self, path, method, base_url):
|
|
link = super().get_link(path, method, base_url)
|
|
# Do something to customize link here...
|
|
return link
|
|
|
|
class MyView(APIView):
|
|
schema = CustomViewSchema()
|
|
</code></pre>
|
|
<p>The following methods are available to override.</p>
|
|
<h3 id="get_linkself-path-method-base_url"><a class="toclink" href="#get_linkself-path-method-base_url">get_link(self, path, method, base_url)</a></h3>
|
|
<p>Returns a <code>coreapi.Link</code> instance corresponding to the given view.</p>
|
|
<p>This is the main entry point.
|
|
You can override this if you need to provide custom behaviors for particular views.</p>
|
|
<h3 id="get_descriptionself-path-method"><a class="toclink" href="#get_descriptionself-path-method">get_description(self, path, method)</a></h3>
|
|
<p>Returns a string to use as the link description. By default this is based on the
|
|
view docstring as described in the "Schemas as Documentation" section above.</p>
|
|
<h3 id="get_encodingself-path-method"><a class="toclink" href="#get_encodingself-path-method">get_encoding(self, path, method)</a></h3>
|
|
<p>Returns a string to indicate the encoding for any request body, when interacting
|
|
with the given view. Eg. <code>'application/json'</code>. May return a blank string for views
|
|
that do not expect a request body.</p>
|
|
<h3 id="get_path_fieldsself-path-method"><a class="toclink" href="#get_path_fieldsself-path-method">get_path_fields(self, path, method):</a></h3>
|
|
<p>Return a list of <code>coreapi.Field()</code> instances. One for each path parameter in the URL.</p>
|
|
<h3 id="get_serializer_fieldsself-path-method"><a class="toclink" href="#get_serializer_fieldsself-path-method">get_serializer_fields(self, path, method)</a></h3>
|
|
<p>Return a list of <code>coreapi.Field()</code> instances. One for each field in the serializer class used by the view.</p>
|
|
<h3 id="get_pagination_fieldsself-path-method"><a class="toclink" href="#get_pagination_fieldsself-path-method">get_pagination_fields(self, path, method)</a></h3>
|
|
<p>Return a list of <code>coreapi.Field()</code> instances, as returned by the <code>get_schema_fields()</code> method on any pagination class used by the view.</p>
|
|
<h3 id="get_filter_fieldsself-path-method"><a class="toclink" href="#get_filter_fieldsself-path-method">get_filter_fields(self, path, method)</a></h3>
|
|
<p>Return a list of <code>coreapi.Field()</code> instances, as returned by the <code>get_schema_fields()</code> method of any filter classes used by the view.</p>
|
|
<h3 id="get_manual_fieldsself-path-method"><a class="toclink" href="#get_manual_fieldsself-path-method">get_manual_fields(self, path, method)</a></h3>
|
|
<p>Return a list of <code>coreapi.Field()</code> instances to be added to or replace generated fields. Defaults to (optional) <code>manual_fields</code> passed to <code>AutoSchema</code> constructor.</p>
|
|
<p>May be overridden to customise manual fields by <code>path</code> or <code>method</code>. For example, a per-method adjustment may look like this:</p>
|
|
<pre><code class="python">def get_manual_fields(self, path, method):
|
|
"""Example adding per-method fields."""
|
|
|
|
extra_fields = []
|
|
if method=='GET':
|
|
extra_fields = # ... list of extra fields for GET ...
|
|
if method=='POST':
|
|
extra_fields = # ... list of extra fields for POST ...
|
|
|
|
manual_fields = super().get_manual_fields(path, method)
|
|
return manual_fields + extra_fields
|
|
</code></pre>
|
|
|
|
<h3 id="update_fieldsfields-update_with"><a class="toclink" href="#update_fieldsfields-update_with">update_fields(fields, update_with)</a></h3>
|
|
<p>Utility <code>staticmethod</code>. Encapsulates logic to add or replace fields from a list
|
|
by <code>Field.name</code>. May be overridden to adjust replacement criteria.</p>
|
|
<h2 id="manualschema"><a class="toclink" href="#manualschema">ManualSchema</a></h2>
|
|
<p>Allows manually providing a list of <code>coreapi.Field</code> instances for the schema,
|
|
plus an optional description.</p>
|
|
<pre><code>class MyView(APIView):
|
|
schema = ManualSchema(fields=[
|
|
coreapi.Field(
|
|
"first_field",
|
|
required=True,
|
|
location="path",
|
|
schema=coreschema.String()
|
|
),
|
|
coreapi.Field(
|
|
"second_field",
|
|
required=True,
|
|
location="path",
|
|
schema=coreschema.String()
|
|
),
|
|
]
|
|
)
|
|
</code></pre>
|
|
<p>The <code>ManualSchema</code> constructor takes two arguments:</p>
|
|
<p><strong><code>fields</code></strong>: A list of <code>coreapi.Field</code> instances. Required.</p>
|
|
<p><strong><code>description</code></strong>: A string description. Optional.</p>
|
|
<p><strong><code>encoding</code></strong>: Default <code>None</code>. A string encoding, e.g <code>application/json</code>. Optional.</p>
|
|
<hr />
|
|
<h2 id="core-api"><a class="toclink" href="#core-api">Core API</a></h2>
|
|
<p>This documentation gives a brief overview of the components within the <code>coreapi</code>
|
|
package that are used to represent an API schema.</p>
|
|
<p>Note that these classes are imported from the <code>coreapi</code> package, rather than
|
|
from the <code>rest_framework</code> package.</p>
|
|
<h3 id="document"><a class="toclink" href="#document">Document</a></h3>
|
|
<p>Represents a container for the API schema.</p>
|
|
<h4 id="title_1"><a class="toclink" href="#title_1"><code>title</code></a></h4>
|
|
<p>A name for the API.</p>
|
|
<h4 id="url_1"><a class="toclink" href="#url_1"><code>url</code></a></h4>
|
|
<p>A canonical URL for the API.</p>
|
|
<h4 id="content"><a class="toclink" href="#content"><code>content</code></a></h4>
|
|
<p>A dictionary, containing the <code>Link</code> objects that the schema contains.</p>
|
|
<p>In order to provide more structure to the schema, the <code>content</code> dictionary
|
|
may be nested, typically to a second level. For example:</p>
|
|
<pre><code>content={
|
|
"bookings": {
|
|
"list": Link(...),
|
|
"create": Link(...),
|
|
...
|
|
},
|
|
"venues": {
|
|
"list": Link(...),
|
|
...
|
|
},
|
|
...
|
|
}
|
|
</code></pre>
|
|
<h3 id="link"><a class="toclink" href="#link">Link</a></h3>
|
|
<p>Represents an individual API endpoint.</p>
|
|
<h4 id="url_2"><a class="toclink" href="#url_2"><code>url</code></a></h4>
|
|
<p>The URL of the endpoint. May be a URI template, such as <code>/users/{username}/</code>.</p>
|
|
<h4 id="action"><a class="toclink" href="#action"><code>action</code></a></h4>
|
|
<p>The HTTP method associated with the endpoint. Note that URLs that support
|
|
more than one HTTP method, should correspond to a single <code>Link</code> for each.</p>
|
|
<h4 id="fields"><a class="toclink" href="#fields"><code>fields</code></a></h4>
|
|
<p>A list of <code>Field</code> instances, describing the available parameters on the input.</p>
|
|
<h4 id="description"><a class="toclink" href="#description"><code>description</code></a></h4>
|
|
<p>A short description of the meaning and intended usage of the endpoint.</p>
|
|
<h3 id="field"><a class="toclink" href="#field">Field</a></h3>
|
|
<p>Represents a single input parameter on a given API endpoint.</p>
|
|
<h4 id="name"><a class="toclink" href="#name"><code>name</code></a></h4>
|
|
<p>A descriptive name for the input.</p>
|
|
<h4 id="required"><a class="toclink" href="#required"><code>required</code></a></h4>
|
|
<p>A boolean, indicated if the client is required to included a value, or if
|
|
the parameter can be omitted.</p>
|
|
<h4 id="location"><a class="toclink" href="#location"><code>location</code></a></h4>
|
|
<p>Determines how the information is encoded into the request. Should be one of
|
|
the following strings:</p>
|
|
<p><strong>"path"</strong></p>
|
|
<p>Included in a templated URI. For example a <code>url</code> value of <code>/products/{product_code}/</code> could be used together with a <code>"path"</code> field, to handle API inputs in a URL path such as <code>/products/slim-fit-jeans/</code>.</p>
|
|
<p>These fields will normally correspond with <a href="https://docs.djangoproject.com/en/stable/topics/http/urls/#named-groups">named arguments in the project URL conf</a>.</p>
|
|
<p><strong>"query"</strong></p>
|
|
<p>Included as a URL query parameter. For example <code>?search=sale</code>. Typically for <code>GET</code> requests.</p>
|
|
<p>These fields will normally correspond with pagination and filtering controls on a view.</p>
|
|
<p><strong>"form"</strong></p>
|
|
<p>Included in the request body, as a single item of a JSON object or HTML form. For example <code>{"colour": "blue", ...}</code>. Typically for <code>POST</code>, <code>PUT</code> and <code>PATCH</code> requests. Multiple <code>"form"</code> fields may be included on a single link.</p>
|
|
<p>These fields will normally correspond with serializer fields on a view.</p>
|
|
<p><strong>"body"</strong></p>
|
|
<p>Included as the complete request body. Typically for <code>POST</code>, <code>PUT</code> and <code>PATCH</code> requests. No more than one <code>"body"</code> field may exist on a link. May not be used together with <code>"form"</code> fields.</p>
|
|
<p>These fields will normally correspond with views that use <code>ListSerializer</code> to validate the request input, or with file upload views.</p>
|
|
<h4 id="encoding"><a class="toclink" href="#encoding"><code>encoding</code></a></h4>
|
|
<p><strong>"application/json"</strong></p>
|
|
<p>JSON encoded request content. Corresponds to views using <code>JSONParser</code>.
|
|
Valid only if either one or more <code>location="form"</code> fields, or a single
|
|
<code>location="body"</code> field is included on the <code>Link</code>.</p>
|
|
<p><strong>"multipart/form-data"</strong></p>
|
|
<p>Multipart encoded request content. Corresponds to views using <code>MultiPartParser</code>.
|
|
Valid only if one or more <code>location="form"</code> fields is included on the <code>Link</code>.</p>
|
|
<p><strong>"application/x-www-form-urlencoded"</strong></p>
|
|
<p>URL encoded request content. Corresponds to views using <code>FormParser</code>. Valid
|
|
only if one or more <code>location="form"</code> fields is included on the <code>Link</code>.</p>
|
|
<p><strong>"application/octet-stream"</strong></p>
|
|
<p>Binary upload request content. Corresponds to views using <code>FileUploadParser</code>.
|
|
Valid only if a <code>location="body"</code> field is included on the <code>Link</code>.</p>
|
|
<h4 id="description_1"><a class="toclink" href="#description_1"><code>description</code></a></h4>
|
|
<p>A short description of the meaning and intended usage of the input field.</p>
|
|
<hr />
|
|
<h1 id="third-party-packages"><a class="toclink" href="#third-party-packages">Third party packages</a></h1>
|
|
<h2 id="drf-yasg-yet-another-swagger-generator"><a class="toclink" href="#drf-yasg-yet-another-swagger-generator">drf-yasg - Yet Another Swagger Generator</a></h2>
|
|
<p><a href="https://github.com/axnsan12/drf-yasg/">drf-yasg</a> generates <a href="https://openapis.org/">OpenAPI</a> documents suitable for code generation - nested schemas,
|
|
named models, response bodies, enum/pattern/min/max validators, form parameters, etc.</p>
|
|
|
|
|
|
</div> <!--/span-->
|
|
</div> <!--/row-->
|
|
</div> <!--/.fluid-container-->
|
|
</div> <!--/.body content-->
|
|
<div id="push"></div>
|
|
</div> <!--/.wrapper -->
|
|
|
|
<footer class="span12">
|
|
<p>Documentation built with <a href="http://www.mkdocs.org/">MkDocs</a>.
|
|
</p>
|
|
</footer>
|
|
|
|
<!-- Le javascript
|
|
================================================== -->
|
|
<!-- Placed at the end of the document so the pages load faster -->
|
|
<script src="../../js/jquery-1.8.1-min.js"></script>
|
|
<script src="../../js/prettify-1.0.js"></script>
|
|
<script src="../../js/bootstrap-2.1.1-min.js"></script>
|
|
<script async src="https://fund.django-rest-framework.org/sidebar_include.js"></script>
|
|
<script>var base_url = '../..';</script>
|
|
<script src="../../mkdocs/js/require.js"></script>
|
|
<script src="../../js/theme.js"></script>
|
|
|
|
<script>
|
|
var shiftWindow = function() {
|
|
scrollBy(0, -50)
|
|
};
|
|
|
|
if (location.hash) shiftWindow();
|
|
window.addEventListener("hashchange", shiftWindow);
|
|
|
|
$('.dropdown-menu').on('click touchstart', function(event) {
|
|
event.stopPropagation();
|
|
});
|
|
|
|
// Dynamically force sidenav/dropdown to no higher than browser window
|
|
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
|
|
|
|
$(function() {
|
|
$(window).resize(function() {
|
|
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |