Metadata
Metadata provides information about your API, such as the supported HTTP methods, expected request formats, and details about the serializers and fields used by a view. This is often accessed via the HTTP OPTIONS method.
When a client sends an OPTIONS request to an endpoint, DRF responds with a dictionary of metadata, which can be used to dynamically generate forms or documentation.
How it works
By default, an OPTIONS request returns metadata containing:
name: View name.description: View description (from the docstring).renders: List of supported media types for responses.parses: List of supported media types for request data.actions: Detailed information about the expected input fields forPOSTandPUTmethods.
Setting Metadata Classes
You can configure the metadata class globally in your settings.py:
REST_FRAMEWORK = {
'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata'
}Or you can configure it on a per-view basis:
Class-Based Views
from rest_framework.metadata import SimpleMetadata
from rest_framework.views import APIView
class ExampleView(APIView):
metadata_class = SimpleMetadataTo completely disable metadata for a view, set metadata_class to None.
Custom Metadata
You can create a custom metadata class to change the information returned by OPTIONS requests. Subclass BaseMetadata and implement the determine_metadata(self, request, view) method.
from rest_framework.metadata import BaseMetadata
class CustomMetadata(BaseMetadata):
def determine_metadata(self, request, view):
return {
'name': view.get_view_name(),
'description': view.get_view_description(),
'custom_field': 'My custom metadata value'
}