A Quick Overview of Django Rest Framework

created:

updated:

tags: django-rest-framework api

Serializer

Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parased data to be converted back int complex types, after first validating the incoming data.

What is Serialization?

Serialization is the process of converting an object’s state to a byte system. This byte stream can then be saved to a file, sent over a network, or stored in a database.

Many different formats can be used for serialization, such as JSON, XML, and binary. JSON and XML are popular formats for serialization because they are human-readable and can be easily parsed by other systems.

What is Deserialization?

Deserialization is the reverse process of serialization. It involves taking a byte system and converting it back to an object.

Validation

When deserializing data, you always need to call is_valid() before attempting to access the validated data, or save an object instance. If any validation errors occur, the .errors property will contain a dictionary representing the resulting error messages.

Views

  • Views handle incoming web requests and return responses.

Generic Views

  • Generic views are class-based views, and class-based views allow us to use reusable behaviour of views’ functionality as they provide numerous pre-built views for commonly used patterns

Some methods:

  • get_queryset(self): “Returns the queryset that should be used for list views, and that should be the base for lookups in detail views. Defaults to returning the queryset specified by the queryset attribute. 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.”
  • get_object(self): “Returns an object instance that should be used for detail views. Defaults to using the lookup_field parameter to filter the base queryset.”
  • get_serializer_class(self): “Returns the class that should be used for the serializer. Defaults to returning the serializer_class attribute. May be overriden to provide dynamic behavior, such as using different serializers for read and write operations, or providing different serializers to different types of users.”

Mixins

Django Rest Framework provides mixin classes that “provide the actions that are used to provide the basic behavior.” They provide action methods (ex: list(), create(), update()) rather than handler methods (ex: get(), post()). Such mixins are ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin.

ViewSet

Viewsets allow us to have the logic for a set of related views in a single class.

A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list(), and .create(). The method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the .as_view() method. Typically, rather than explicitly registering the views in a viewset in the urlconf, you’ll register the viewset with a router class, that automatically determins the urlconf for you.

Router

Django Rest Framework provides automatic URL routing which wires Django’s view logic to a set of URLs.

References