APIView
The base class for all views in REST framework. Signature:class APIView(View)
APIView classes differ from regular Django View classes in the following ways:
- Requests passed to handler methods are REST framework’s
Requestinstances, not Django’sHttpRequest - Handler methods may return REST framework’s
Response, instead of Django’sHttpResponse - Any
APIExceptionexceptions will be caught and mediated into appropriate responses - Incoming requests will be authenticated and appropriate permission and/or throttle checks will be run before dispatching to the handler method
Class Attributes
renderer_classes
A list of renderer classes that determine the media types that can be returned. Default:api_settings.DEFAULT_RENDERER_CLASSES
parser_classes
A list of parser classes that determine which media types this view can accept. Default:api_settings.DEFAULT_PARSER_CLASSES
authentication_classes
A list of authentication classes that are used to authenticate the request. Default:api_settings.DEFAULT_AUTHENTICATION_CLASSES
throttle_classes
A list of throttle classes that determine the rate limits for requests. Default:api_settings.DEFAULT_THROTTLE_CLASSES
permission_classes
A list of permission classes that determine whether a request should be permitted. Default:api_settings.DEFAULT_PERMISSION_CLASSES
content_negotiation_class
The class used for content negotiation. Default:api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS
metadata_class
The class used for determining metadata in OPTIONS responses. Default:api_settings.DEFAULT_METADATA_CLASS
versioning_class
The class used for determining the API version of incoming requests. Default:api_settings.DEFAULT_VERSIONING_CLASS
schema
The schema for this view, used for automatic schema generation. Default:DefaultSchema()
Class Methods
as_view()
Signature:as_view(**initkwargs)
Returns a callable view that can be used in URL patterns.
Keyword arguments to pass to the view instance.
Properties
allowed_methods
Returns a list of HTTP methods that this view allows. Returns:list[str]
default_response_headers
Returns a dictionary of default headers for responses. Returns:dict
Instance Methods
dispatch()
Signature:dispatch(request, *args, **kwargs)
The main entry point for handling requests. Similar to Django’s dispatch, but with extra hooks for startup, finalize, and exception handling.
The Django HTTP request.
Positional URL arguments.
Named URL arguments.
HttpResponseBase
initial()
Signature:initial(request, *args, **kwargs)
Runs anything that needs to occur prior to calling the handler method. Performs content negotiation, authentication, permission checks, and throttling.
The REST framework request.
Positional URL arguments.
Named URL arguments.
initialize_request()
Signature:initialize_request(request, *args, **kwargs)
Returns the initial Request object.
The Django HTTP request.
Positional URL arguments.
Named URL arguments.
Request
finalize_response()
Signature:finalize_response(request, response, *args, **kwargs)
Returns the final response object, ensuring the correct content type is set.
The REST framework request.
The response to finalize.
Positional URL arguments.
Named URL arguments.
Response
handle_exception()
Signature:handle_exception(exc)
Handles any exception that occurs, returning an appropriate Response instance or re-raising the error.
The exception to handle.
Response
permission_denied()
Signature:permission_denied(request, message=None, code=None)
Raises an appropriate exception if the request is not permitted.
The REST framework request.
The error message.
The error code.
NotAuthenticated or PermissionDenied
throttled()
Signature:throttled(request, wait)
Raises a Throttled exception if the request is throttled.
The REST framework request.
The number of seconds to wait before retrying.
Throttled
API Policy Methods
These methods instantiate and return the various API policy classes.get_renderers()
Signature:get_renderers()
Instantiates and returns the list of renderers that this view can use.
Returns: list[BaseRenderer]
get_parsers()
Signature:get_parsers()
Instantiates and returns the list of parsers that this view can use.
Returns: list[BaseParser]
get_authenticators()
Signature:get_authenticators()
Instantiates and returns the list of authenticators that this view can use.
Returns: list[BaseAuthentication]
get_permissions()
Signature:get_permissions()
Instantiates and returns the list of permissions that this view requires.
Returns: list[BasePermission]
get_throttles()
Signature:get_throttles()
Instantiates and returns the list of throttles that this view uses.
Returns: list[BaseThrottle]
get_content_negotiator()
Signature:get_content_negotiator()
Instantiates and returns the content negotiation class to use.
Returns: BaseContentNegotiation
get_exception_handler()
Signature:get_exception_handler()
Returns the exception handler that this view uses.
Returns: callable
Permission & Throttle Checks
check_permissions()
Signature:check_permissions(request)
Checks if the request should be permitted. Raises an appropriate exception if not permitted.
The REST framework request.
PermissionDenied or NotAuthenticated
check_object_permissions()
Signature:check_object_permissions(request, obj)
Checks if the request should be permitted for a given object. Raises an appropriate exception if not permitted.
The REST framework request.
The object to check permissions for.
PermissionDenied
check_throttles()
Signature:check_throttles(request)
Checks if the request should be throttled. Raises an appropriate exception if throttled.
The REST framework request.
Throttled
perform_authentication()
Signature:perform_authentication(request)
Performs authentication on the incoming request.
The REST framework request.
If you override this and simply pass, authentication will be performed lazily when
request.user or request.auth is accessed.Content Negotiation
perform_content_negotiation()
Signature:perform_content_negotiation(request, force=False)
Determines which renderer and media type to use to render the response.
The REST framework request.
If True, returns the first renderer even if negotiation fails.
tuple[BaseRenderer, str] - A tuple of (renderer, media_type)
Context Methods
get_parser_context()
Signature:get_parser_context(http_request)
Returns a dict that is passed to Parser.parse() as the parser_context keyword argument.
The Django HTTP request.
dict - Contains view, args, and kwargs keys
get_renderer_context()
Signature:get_renderer_context()
Returns a dict that is passed to Renderer.render() as the renderer_context keyword argument.
Returns: dict - Contains view, args, kwargs, and request keys
get_exception_handler_context()
Signature:get_exception_handler_context()
Returns a dict that is passed to the exception handler as the context argument.
Returns: dict - Contains view, args, kwargs, and request keys
View Introspection
get_view_name()
Signature:get_view_name()
Returns the view name, as used in OPTIONS responses and in the browsable API.
Returns: str
get_view_description()
Signature:get_view_description(html=False)
Returns descriptive text for the view, as used in OPTIONS responses and in the browsable API.
If True, returns HTML-formatted description.
str
HTTP Method Handlers
options()
Signature:options(request, *args, **kwargs)
Handler method for HTTP OPTIONS requests.
The REST framework request.
Positional URL arguments.
Named URL arguments.
Response
Function-Based Views
@api_view()
Signature:@api_view(http_method_names=['GET'])
Decorator that converts a function-based view into an API view that receives Request instances and can return Response instances.
List of HTTP methods that the view should respond to.
By default, only GET methods will be accepted. Other methods will respond with “405 Method Not Allowed”.
Policy Decorators
These decorators must come after (below) the@api_view decorator.
@renderer_classes()
Signature:@renderer_classes(renderer_class_list)
Sets the renderer classes for the view.
List of renderer classes.
@parser_classes()
Signature:@parser_classes(parser_class_list)
Sets the parser classes for the view.
List of parser classes.
@authentication_classes()
Signature:@authentication_classes(authentication_class_list)
Sets the authentication classes for the view.
List of authentication classes.
@throttle_classes()
Signature:@throttle_classes(throttle_class_list)
Sets the throttle classes for the view.
List of throttle classes.
@permission_classes()
Signature:@permission_classes(permission_class_list)
Sets the permission classes for the view.
List of permission classes.
@content_negotiation_class()
Signature:@content_negotiation_class(content_negotiation_class)
Sets the content negotiation class for the view.
The content negotiation class to use.
@metadata_class()
Signature:@metadata_class(metadata_class)
Sets the metadata class for the view. Used for OPTIONS requests.
The metadata class to use.
@versioning_class()
Signature:@versioning_class(versioning_class)
Sets the versioning class for the view.
The versioning class to use.
@schema()
Signature:@schema(view_inspector)
Sets the schema inspector for the view. Used for OpenAPI schema generation.
The schema inspector class to use, or None to exclude from schema.
Utility Functions
get_view_name()
Signature:get_view_name(view)
Given a view instance, returns a textual name to represent the view. Used in the browsable API and OPTIONS responses.
The view instance.
str
This function is the default for the VIEW_NAME_FUNCTION setting.
get_view_description()
Signature:get_view_description(view, html=False)
Given a view instance, returns a textual description to represent the view. Used in the browsable API and OPTIONS responses.
The view instance.
If True, returns HTML-formatted description.
str
This function is the default for the VIEW_DESCRIPTION_FUNCTION setting.
exception_handler()
Signature:exception_handler(exc, context)
Returns the response that should be used for any given exception.
The exception to handle.
Context dict containing view, args, kwargs, and request.
Response | None
By default, handles APIException, Django’s Http404, and PermissionDenied exceptions. Unhandled exceptions return None, which causes a 500 error to be raised.
