Skip to content

Django Rest Framework - Details

Responses

  • the Response class cannot natively handle complex datatypes such as Django model instances, so you need to serialize the data before creating the Response object.

Views

.handle_exception()

Any exception thrown by the handler method will be passed to this method - returns a Response instance, or re-raises the exception - to customize the error responses, subclass this method

The default implementation handles any subclass of rest_framework.exceptions.APIException, as well as Django's Http404 and PermissionDenied exceptions, and returns an appropriate error response.

Policy Decorators

The ones that end with _class expect a single class while the ones ending in _classes expect a list or tuple of classes.

py
@renderer_classes(...)
@parser_classes(...)
@authentication_classes(...)
@throttle_classes(...)
@permission_classes(...)
@content_negotiation_class(...)
@metadata_class(...)
@versioning_class(...)

GenericAPIView

Base Classes

Concrete view classes can be imported from rest_framework.generics. It is also possible to create custom base classes that inherit class functionality from one of the concrete views.

Attributes for Controlling View Behavior

  • queryset: Used for returning objects from this view. Typically, you must either set this attribute, or override the get_queryset() method.
    • If overridden it is important that you call get_queryset() instead of accessing this property directly

      Anti-pattern

      py
      self.queryset

      Best Practice

      py
      self.get_queryset()
    • when overridding the listing can be optimized to avoid N + 1 queries

      • select_related: will fetch related objects in the same query
      • prefetch_related: will efficiently load collections of related objects
      • select_related and prefetch_related can be chained
  • serializer_class: The serializer class that should be used for validating and deserializing input, and for serializing output
  • lookup_field: The model field used for performing object lookup of individual model instances, defaults to 'pk'
  • lookup_url_kwarg: The URL keyword argument that should be used for object lookup. Defaults to using the same value as lookup_field.

Save and Delete Hooks

  • perform_create, perform_update, perform_destroy
    • particularly useful when setting attributes that are in the request but not the request data like request.user
    • also useful for adding behavior that occurs before or after saving an object like logging the change or sending an email
    • lastly they are a good place to add additional validation by adding ValidationError conditions

Mixins

There are mixins that can be imported from rest_framework.mixins but it is possible to make custom mixins that can be inherited by views.

Classes and Mixins

It is common to combine custom classes and mixins to create desired custom functionality.

Viewsets

Larger topic

Routers

Larger topic

Parsers

Small topic

Renderers

Large topic

Serializers

Very large topic

Serializer Fields

Very large topic

Serializer Relations

Very large topic

Validators

Large topic

Authentication

Large topic

Permissions

Large topic

Caching

Small topic

Throttling

Small topic

Filtering

Large topic

Pagination

Large topic

Versioning

Small topic

Content Negotiation

Small topic

Metadata

Small topic

Format Suffixes

Small topic

Returning URLs

Small topic

Exceptions

Small topic

Testing

Very large topic

Settings

Very large topic