Official original text link this series of articles github address reprint please indicate the source
metadata
The REST framework includes a configurable mechanism for determining how the API responds to OPTIONS requests. This allows you to return API Schema or other resource information.
There is no widely adopted convention for what style of response HTTP OPTIONS requests should return, so we provide a specialized style to return some useful information.
Here is a sample response that demonstrates the information returned by default.
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
{
"name": "To Do List"."description": "List existing 'To Do' items, or create a new item."."renders": [
"application/json"."text/html"]."parses": [
"application/json"."application/x-www-form-urlencoded"."multipart/form-data"]."actions": {
"POST": {
"note": {
"type": "string"."required": false,
"read_only": false,
"label": "title"."max_length": 100}}}}Copy the code
Set the metadata scheme
You can use the ‘DEFAULT_METADATA_CLASS’ Settings key to set the global metadata class:
REST_FRAMEWORK = {
'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata'
}
Copy the code
Or you can set a separate view metadata class:
class APIRoot(APIView):
metadata_class = APIRootMetadata
def get(self, request, format=None):
return Response({
...
})
Copy the code
The REST Framework package contains only one metadata class implementation named SimpleMetadata. If you want to use a different style, you need to implement a custom metadata class.
Creating a Schema endpoint
If you have specific requirements for creating schema endpoints accessed through regular GET requests, consider reusing the metadata API for this purpose.
For example, you can use the following additional routes on a view set to provide a linkable Schema endpoint.
@list_route(methods=['GET'])
def schema(self, request):
meta = self.metadata_class()
data = meta.determine_metadata(request, self)
return Response(data)
Copy the code
There are several reasons to choose this approach, including that OPTIONS responses cannot be cached.
Custom metadata classes
If you want to provide a custom metadata class, you should extend BaseMetadata and implement determine_metadata(self, Request, View).
Some of the things you might want to do include returning schema information, using formats such as JSON Schema, or returning debugging information to the administrator user.
Take a chestnut
The following classes can be used to qualify the information returned to the OPTIONS request.
class MinimalMetadata(BaseMetadata):
""" Don't include field and other information for `OPTIONS` requests. Just return the name and description. """
def determine_metadata(self, request, view):
return {
'name': view.get_view_name(),
'description': view.get_view_description()
}
Copy the code
Then configure your Settings to use this custom class:
REST_FRAMEWORK = {
'DEFAULT_METADATA_CLASS': 'myproject.apps.core.MinimalMetadata'
}
Copy the code