Views Mixins

Pytthon-Rest-Framework provides views mixins, for easy customization of your API.

View Mixins is a powerful Python-Rest-Framework tool, with which you can very flexibly configure only the functionality that you need.

Each mixin provides its own functionality and asks for its settings. Views Mixins do not overlap in functionality, so you can use any combination of them.

Views Mixins is not only common for all types of frameworks, but also specific for each framework.

All Mixins can be found here:

from rest_framework.views.mixins import *

Mixins


GetRequestAbstractMixin

Basic mixin, which is an asbestos class and requires the implementation of:

Example(Flask):

from flask import request, jsonify, make_response
from flask.views import View
from rest_framework.views.mixins import GetRequestAbstractMixin


class MyView(View, GetRequestAbstractMixin):

    @property
    def request_object(self):
        return request

Example(Flask):

from flask import request, jsonify, make_response
from flask.views import View
from rest_framework.views.mixins import GetRequestAbstractMixin


class MyView(View, GetRequestAbstractMixin):

    @property
    def current_request_method(self):
        return request.method

GetSerializerMixin

Mixin, which allows you to conveniently work with serializers.

Provides methods: get_serializer,get_request_serializer, get_response_serializer. All these methods look for serializers in the class attribute serializer_classes (Dict) according to certain rules:

from rest_framework.views import GetSerializerMixin

class ExampleView(GetSerializerMixin):
    serializer_classes = {
        'get': None,
        'post': None
    }

Note: You must add the methods you want to use in your view.


from rest_framework.views import GetSerializerMixin

class ExampleView(GetSerializerMixin):
    serializer_classes = {
        'get': GetSerializer,
        'post': {
            'in': PostRequestSerializer
        },
        'put': {
            'in': PutRequestSerializer,
            'out': PutResponseSerializer
        }
    }

.get_request_serializer()

A method that returns a Serializer to process data from a client.

from rest_framework.views import GetSerializerMixin

class ExampleView(GetSerializerMixin):
    serializer_classes = {
        'get': GetSerializer,
        'post': {
            'in': PostRequestSerializer
        },
        'put': {
            'in': PutRequestSerializer,
            'out': PutResponseSerializer
        },
        'patch': {}
    }

    def get(self):
        ser = self.get_request_serializer()
        # ser - GetSerializer

    def post(self):
        ser = self.get_request_serializer()
        # ser - PostRequestSerializer

    def put(self):
        ser = self.get_request_serializer()
        # ser - PutRequestSerializer

    def patch(self):
        ser = self.get_request_serializer()
        # ser - None

    def delete(self):
        ser = self.get_request_serializer()
        # ser - None

.get_response_serializer()

A method that returns a Serializer to serialize data to return to a user. For Response.

from rest_framework.views import GetSerializerMixin

class ExampleView(GetSerializerMixin):
    serializer_classes = {
        'get': GetSerializer,
        'post': {
            'in': PostRequestSerializer
        },
        'put': {
            'in': PutRequestSerializer,
            'out': PutResponseSerializer
        },
        'patch': {}
    }

    def get(self):
        ser = self.get_response_serializer()
        # ser - GetSerializer

    def post(self):
        ser = self.get_response_serializer()
        # ser - None

    def put(self):
        ser = self.get_response_serializer()
        # ser - PutResponseSerializer

    def patch(self):
        ser = self.get_response_serializer()
        # ser - None

    def delete(self):
        ser = self.get_response_serializer()
        # ser - None

.get_serializer()

Method for search Serializer.

Signature: get_serializer(key: str)

Example:

from rest_framework.views import GetSerializerMixin

class ExampleView(GetSerializerMixin):
    serializer_classes = {
        'get': GetSerializer,
        'post': {
            'in': PostRequestSerializer
        },
        'put': {
            'in': PutRequestSerializer,
            'out': PutResponseSerializer
        },
        'patch': {}
    }

    def get(self):
        ser = self.get_serializer('in')
        # ser - GetSerializer
        ser = self.get_serializer('out')
        # ser - GetSerializer

    def post(self):
        ser = self.get_serializer('in')
        # ser - PostRequestSerializer
        ser = self.get_serializer('out')
        # ser - None

    def put(self):
        ser = self.get_serializer('in')
        # ser - PutRequestSerializer
        ser = self.get_serializer('out')
        # ser - PutResponseSerializer

    def patch(self):
        ser = self.get_serializer('in')
        # ser - None
        ser = self.get_serializer('out')
        # ser - None

    def delete(self):
        ser = self.get_serializer('in')
        # ser - None
        ser = self.get_serializer('out')
        # ser - None

GetResponseMixin

Mixin for easy response to the client.

Attributes:

.get_response()

Formation and preparation of response Response object for the client.

Signature: .get_response(obj=None, is_serialized=True, status_code=200)

Example(AioHTTP):

from aiohttp.web import View, json_response
from rest_framework.views import GetResponseMixin

class ExampleView(View, GetResponseMixin):
    response_class = json_response
    serializer_classes = {
        'get': MyGetSerializer
    }

    async def get(self):
        # Your code
        data = MyModel()
        return self.get_response(data, is_serialized=True, status_code=200)

.get_list_response()

Formation and preparation of response Many Response objects for the client.

Signature: get_list_response(obj=None, is_serialized=True, status_code=200, *args, **kwargs)

Example(AioHTTP):

from aiohttp.web import View, json_response
from rest_framework.views import GetResponseMixin

class ExampleView(View, GetResponseMixin):
    response_class = json_response
    serializer_classes = {
        'get': MyGetSerializer
    }

    async def get(self):
        # Your code
        data = [MyModel(), MyModel(), MyModel()]
        return self.get_list_response(data, is_serialized=True, status_code=200)

Writing custom FrameworkBaseView

To write your Mixin, you can inherit from GetRequestAbstractMixin if you need the propertiesrequest_object, request_object_method. From any other Mixin, if you need its methods or attributes. And from object, if your Mixin does not depend on the existing ones.

Example:

class CustomHeadersMixin(object):
    def update_headers(self, headers):
        headers.update({'Custom-Auth-Header': 'HashCode'})
        return headers