| 1 |
--- |
| 2 |
source: |
| 3 |
- metadata.py |
| 4 |
--- |
| 5 |
|
| 6 |
# Metadata |
| 7 |
|
| 8 |
> [The `OPTIONS`] method allows a client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval. |
| 9 |
> |
| 10 |
> — [RFC7231, Section 4.3.7.][cite] |
| 11 |
|
| 12 |
REST framework includes a configurable mechanism for determining how your API should respond to `OPTIONS` requests. This allows you to return API schema or other resource information. |
| 13 |
|
| 14 |
There are not currently any widely adopted conventions for exactly what style of response should be returned for HTTP `OPTIONS` requests, so we provide an ad-hoc style that returns some useful information. |
| 15 |
|
| 16 |
Here's an example response that demonstrates the information that is returned by default. |
| 17 |
|
| 18 |
HTTP 200 OK |
| 19 |
Allow: GET, POST, HEAD, OPTIONS |
| 20 |
Content-Type: application/json |
| 21 |
|
| 22 |
{ |
| 23 |
"name": "To Do List", |
| 24 |
"description": "List existing 'To Do' items, or create a new item.", |
| 25 |
"renders": [ |
| 26 |
"application/json", |
| 27 |
"text/html" |
| 28 |
], |
| 29 |
"parses": [ |
| 30 |
"application/json", |
| 31 |
"application/x-www-form-urlencoded", |
| 32 |
"multipart/form-data" |
| 33 |
], |
| 34 |
"actions": { |
| 35 |
"POST": { |
| 36 |
"note": { |
| 37 |
"type": "string", |
| 38 |
"required": false, |
| 39 |
"read_only": false, |
| 40 |
"label": "title", |
| 41 |
"max_length": 100 |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
## Setting the metadata scheme |
| 48 |
|
| 49 |
You can set the metadata class globally using the `'DEFAULT_METADATA_CLASS'` settings key: |
| 50 |
|
| 51 |
REST_FRAMEWORK = { |
| 52 |
'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata' |
| 53 |
} |
| 54 |
|
| 55 |
Or you can set the metadata class individually for a view: |
| 56 |
|
| 57 |
class APIRoot(APIView): |
| 58 |
metadata_class = APIRootMetadata |
| 59 |
|
| 60 |
def get(self, request, format=None): |
| 61 |
return Response({ |
| 62 |
... |
| 63 |
}) |
| 64 |
|
| 65 |
The REST framework package only includes a single metadata class implementation, named `SimpleMetadata`. If you want to use an alternative style you'll need to implement a custom metadata class. |
| 66 |
|
| 67 |
## Creating schema endpoints |
| 68 |
|
| 69 |
If you have specific requirements for creating schema endpoints that are accessed with regular `GET` requests, you might consider reusing the metadata API for doing so. |
| 70 |
|
| 71 |
For example, the following additional route could be used on a viewset to provide a linkable schema endpoint. |
| 72 |
|
| 73 |
@action(methods=['GET'], detail=False) |
| 74 |
def api_schema(self, request): |
| 75 |
meta = self.metadata_class() |
| 76 |
data = meta.determine_metadata(request, self) |
| 77 |
return Response(data) |
| 78 |
|
| 79 |
There are a couple of reasons that you might choose to take this approach, including that `OPTIONS` responses [are not cacheable][no-options]. |
| 80 |
|
| 81 |
--- |
| 82 |
|
| 83 |
## Custom metadata classes |
| 84 |
|
| 85 |
If you want to provide a custom metadata class you should override `BaseMetadata` and implement the `determine_metadata(self, request, view)` method. |
| 86 |
|
| 87 |
Useful things that you might want to do could include returning schema information, using a format such as [JSON schema][json-schema], or returning debug information to admin users. |
| 88 |
|
| 89 |
### Example |
| 90 |
|
| 91 |
The following class could be used to limit the information that is returned to `OPTIONS` requests. |
| 92 |
|
| 93 |
class MinimalMetadata(BaseMetadata): |
| 94 |
""" |
| 95 |
Don't include field and other information for `OPTIONS` requests. |
| 96 |
Just return the name and description. |
| 97 |
""" |
| 98 |
def determine_metadata(self, request, view): |
| 99 |
return { |
| 100 |
'name': view.get_view_name(), |
| 101 |
'description': view.get_view_description() |
| 102 |
} |
| 103 |
|
| 104 |
Then configure your settings to use this custom class: |
| 105 |
|
| 106 |
REST_FRAMEWORK = { |
| 107 |
'DEFAULT_METADATA_CLASS': 'myproject.apps.core.MinimalMetadata' |
| 108 |
} |
| 109 |
|
| 110 |
## Third party packages |
| 111 |
|
| 112 |
The following third party packages provide additional metadata implementations. |
| 113 |
|
| 114 |
### DRF-schema-adapter |
| 115 |
|
| 116 |
[drf-schema-adapter][drf-schema-adapter] is a set of tools that makes it easier to provide schema information to frontend frameworks and libraries. It provides a metadata mixin as well as 2 metadata classes and several adapters suitable to generate [json-schema][json-schema] as well as schema information readable by various libraries. |
| 117 |
|
| 118 |
You can also write your own adapter to work with your specific frontend. |
| 119 |
If you wish to do so, it also provides an exporter that can export those schema information to json files. |
| 120 |
|
| 121 |
[cite]: https://tools.ietf.org/html/rfc7231#section-4.3.7 |
| 122 |
[no-options]: https://www.mnot.net/blog/2012/10/29/NO_OPTIONS |
| 123 |
[json-schema]: https://json-schema.org/ |
| 124 |
[drf-schema-adapter]: https://github.com/drf-forms/drf-schema-adapter |