rest_framework.generics

Generic views that provide commonly needed behaviour.

Module Contents

get_object_or_404(queryset, *filter_args, **filter_kwargs)

Same as Django’s standard shortcut, but make sure to also raise 404 if the filter_kwargs don’t match the required types.

class GenericAPIView

Bases:rest_framework.views.APIView

Base class for all other generic views.

queryset
serializer_class
lookup_field = pk
lookup_url_kwarg
filter_backends
pagination_class
paginator

The paginator instance associated with the view, or None.

get_queryset(self)

Get the list of items for this view. This must be an iterable, and may be a queryset. Defaults to using self.queryset.

This method should always be used rather than accessing self.queryset directly, as self.queryset gets evaluated only once, and those results are cached for all subsequent requests.

You may want to override this if you need to provide different querysets depending on the incoming request.

(Eg. return a list of items that is specific to the user)

get_object(self)

Returns the object the view is displaying.

You may want to override this if you need to provide non-standard queryset lookups. Eg if objects are referenced using multiple keyword arguments in the url conf.

get_serializer(self, *args, **kwargs)

Return the serializer instance that should be used for validating and deserializing input, and for serializing output.

get_serializer_class(self)

Return the class to use for the serializer. Defaults to using self.serializer_class.

You may want to override this if you need to provide different serializations depending on the incoming request.

(Eg. admins get full serialization, others get basic serialization)

get_serializer_context(self)

Extra context provided to the serializer class.

filter_queryset(self, queryset)

Given a queryset, filter it with whichever filter backend is in use.

You are unlikely to want to override this method, although you may need to call it either from a list view, or from a custom get_object method if you want to apply the configured filtering backend to the default queryset.

paginate_queryset(self, queryset)

Return a single page of results, or None if pagination is disabled.

get_paginated_response(self, data)

Return a paginated style Response object for the given output data.

class CreateAPIView

Bases:rest_framework.mixins.CreateModelMixin, rest_framework.generics.GenericAPIView

Concrete view for creating a model instance.

post(self, request, *args, **kwargs)
class ListAPIView

Bases:rest_framework.mixins.ListModelMixin, rest_framework.generics.GenericAPIView

Concrete view for listing a queryset.

get(self, request, *args, **kwargs)
class RetrieveAPIView

Bases:rest_framework.mixins.RetrieveModelMixin, rest_framework.generics.GenericAPIView

Concrete view for retrieving a model instance.

get(self, request, *args, **kwargs)
class DestroyAPIView

Bases:rest_framework.mixins.DestroyModelMixin, rest_framework.generics.GenericAPIView

Concrete view for deleting a model instance.

delete(self, request, *args, **kwargs)
class UpdateAPIView

Bases:rest_framework.mixins.UpdateModelMixin, rest_framework.generics.GenericAPIView

Concrete view for updating a model instance.

put(self, request, *args, **kwargs)
patch(self, request, *args, **kwargs)
class ListCreateAPIView

Bases:rest_framework.mixins.ListModelMixin, rest_framework.mixins.CreateModelMixin, rest_framework.generics.GenericAPIView

Concrete view for listing a queryset or creating a model instance.

get(self, request, *args, **kwargs)
post(self, request, *args, **kwargs)
class RetrieveUpdateAPIView

Bases:rest_framework.mixins.RetrieveModelMixin, rest_framework.mixins.UpdateModelMixin, rest_framework.generics.GenericAPIView

Concrete view for retrieving, updating a model instance.

get(self, request, *args, **kwargs)
put(self, request, *args, **kwargs)
patch(self, request, *args, **kwargs)
class RetrieveDestroyAPIView

Bases:rest_framework.mixins.RetrieveModelMixin, rest_framework.mixins.DestroyModelMixin, rest_framework.generics.GenericAPIView

Concrete view for retrieving or deleting a model instance.

get(self, request, *args, **kwargs)
delete(self, request, *args, **kwargs)
class RetrieveUpdateDestroyAPIView

Bases:rest_framework.mixins.RetrieveModelMixin, rest_framework.mixins.UpdateModelMixin, rest_framework.mixins.DestroyModelMixin, rest_framework.generics.GenericAPIView

Concrete view for retrieving, updating or deleting a model instance.

get(self, request, *args, **kwargs)
put(self, request, *args, **kwargs)
patch(self, request, *args, **kwargs)
delete(self, request, *args, **kwargs)