Flask Views

AioHTTP has one BaseClassViews - View. Python-Rest-Framework is connected to both.

Note: To use AioHttpViews you need to install aiohttp.

All Classes can be found here:

from rest_framework.views.aiohttp import *

AioHttpBaseViews


AioHTTPApiView

This class is a successor: aiohttp.web.View,rest_framework.views.BaseApiView.

What the class consists of:

from aiohttp.web import json_response

Example:

from rest_framework.views.aiohttp import AioHTTPApiView


class MyView(AioHTTPApiView):
    async def get(self):
        # your view code

AioHttp Mixins

Mixins for AioHttp ​​are the same useful mixins as rest_framework.views.mixins, but they can only be used in AioHttp.


GetValidJsonMixin

The class is successor: rest_framework.views.GetSerializerMixin

Mixin adds the .get_valid_json() method, which parses the query and body of the query, merges them, and automatically serializes it and validates it with rest_framework.serializers.Serializer. You can describe the serializers themselves in the serializers_classes class attribute.

.get_valid_json()

This method retrieves from the request all the data that the user sent. Serializes and validates them and returns as a dictionary.

Signature: .get_valid_json(parse_query=False, raise_exception=True) -> dict

Example:

from rest_framework.views.aiohttp import AioHTTPApiView, GetValidJsonMixin

class MyView(AioHTTPApiView, GetValidJsonMixin):
    serializer_classes = {
        'get': MySerializer()
    }
    async def get(self):
        request_data = await self.get_valid_json()

#############
### Equivalent to
#############

from rest_framework.views.aiohttp import AioHTTPApiView
from rest_framework.views.mixins import GetSerializerMixin

class MyView(AioHTTPApiView, GetSerializerMixin):
    async def get(self):
        serializer_class = self.get_request_serializer()
        try:
            data = await self.request_object.json()
        except Exception:
            data = {}
        ser = serializer_class(data=data)
        ser.is_valid(raise_exception=True)
        request_data = ser.validated_data


AioHttpGenericViews

GenericViews are ready-to-use classes, compiled from base classes and mixins for various needs.


GetSerializerApiGenericView

Generic Api view for GetSerializer methods.

Parents:


GetResponseApiGenericView

Generic Api view for GetResponse methods.

Parents:


GetValidJsonApiGenericView

Generic Api view for GetValidJson methods.

Parents:


ApiGenericView

Generic Api view for all mixins methods.

Parents: