view
The main role of Django REST framwork offers views:
- Serializer execution control (inspection, storage, data conversion)
- Execution control database query
2 view of the base class
APIView
rest_framework.views.APIView
APIView
Is the base class for all views provided REST framework, Django inherited from the View
parent class.
APIView
With View
the exception that:
- Passed to the view method is REST framework of
Request
object instead of DjangoHttpRequeset
objects; - The method may return REST framework view of
Response
the object, the view in response to the data set (the render) conform to the format required by the front end; - Any
APIException
exceptions are captured, and information into appropriate response; - Performing dispatch () prior to distribution, will request authentication, authorization check, flow control.
Support defined attributes
- authentication_classes list or Ganso, authentication type
- permissoin_classes list or Ganso, permission checks class
- throttle_classes lists or tuples, flow control class
In APIView
the view definition is still a conventional method to achieve the class get (), post () method or other requests embodiment.
For example:
from rest_framework.views import APIView
from rest_framework.response import Response
# url(r'^books/$', views.BookListView.as_view()),
class BookListView(APIView):
def get(self, request):
books = BookInfo.objects.all()
serializer = BookInfoSerializer(books, many=True)
return Response(serializer.data)
GenericAPIView [General view class]
rest_framework.generics.GenericAPIView
Inherited from APIVIew
, the main method of operation increases the serialization and database query role is to provide for the implementation of the following methods Mixin extension class support. Typically, when used, may be extended with one or more Mixin classes.
Properties and methods of use on a sequence provided
Attributes:
- serializer_class specified serializer view used
method:
get_serializer_class(self)
Occurs when a plurality of the view class calls the serializer, the condition can be determined by get_serializer_class process by returning a different sequence of class names can allow different views of a method of performing a sequence of objects.
Back serializer class returned by default
serializer_class
can be overridden, for example:def get_serializer_class(self): if self.request.user.is_staff: return FullAccountSerializer return BasicAccountSerializer
get_serializer(self, args, *kwargs)
Returns the serialized object, is mainly used to provide extended Mixin class to use, if we want to get serialized object in view, you can also call this method directly.
Note that, when the method of providing a sequence of objects, attributes will be added to the context object serialization three data: request, format, view, these three data objects can be used to define the serializer.
- request the current request object view
- view class the current view object request
- Returns the current request format desired data format
Properties and methods on database queries provided
Attributes:
- queryset indicating that the data set used queries
method:
get_queryset(self)
Return query set used by the view, is mainly used to provide extended Mixin class use, is the basis for access to data and list view details view, return to the default
queryset
attributes can be overridden, for example:def get_queryset(self): user = self.request.user return user.accounts.all()
get_object(self)
Back Detail view data object model classes required, primarily to provide for the use of extension class Mixin.
In an attempt can call this method to get the model class object details information.
If the model class object details visit does not exist, it will return 404.
This method checks whether the current object has permission to be accessed using check_object_permissions method APIView provided by default.
For example:
# url(r'^books/(?P<pk>\d+)/$', views.BookDetailView.as_view()), class BookDetailView(GenericAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer def get(self, request, pk): book = self.get_object() # get_object()方法根据pk参数查找queryset中的数据对象 serializer = self.get_serializer(book) return Response(serializer.data)
Other properties that can be set
- pagination_class specified class control tab
- filter_backends specified backend control filter
5 views extended by
effect:
It provides several back-end view (deletion of data resources has been checked) to achieve processing flow, if you need to write views belong to these five, you can view the code reuse through inheritance corresponding extension classes, reduce code I have written the amount.
This extension classes need to match five GenericAPIView parent class, because class methods to achieve five expansion serializer and database queries GenericAPIView provided need to call.
1)ListModelMixin
Extension class list view, there is provided list(request, *args, **kwargs)
method to quickly list view 200 returns a status code.
The data will Mixin list method for filtering and paging.
Source:
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
# 过滤
queryset = self.filter_queryset(self.get_queryset())
# 分页
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
# 序列化
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
For example:
from rest_framework.mixins import ListModelMixin
class BookListView(ListModelMixin, GenericAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
def get(self, request):
return self.list(request)
2)CreateModelMixin
Creating the view extension class that provides create(request, *args, **kwargs)
a method to quickly realize the resources to create a view, a successful return 201 status code.
If the sequence of the authentication failure of the data transmission of the front end, returns 400 an error.
Source:
class CreateModelMixin(object):
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
# 获取序列化器
serializer = self.get_serializer(data=request.data)
# 验证
serializer.is_valid(raise_exception=True)
# 保存
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
serializer.save()
def get_success_headers(self, data):
try:
return {'Location': str(data[api_settings.URL_FIELD_NAME])}
except (TypeError, KeyError):
return {}
3) RetrieveModelMixin
Extension class detail view, there is provided retrieve(request, *args, **kwargs)
a method, it can be implemented quickly return to a present data object.
If so, return to 200, otherwise 404.
Source:
class RetrieveModelMixin(object):
"""
Retrieve a model instance.
"""
def retrieve(self, request, *args, **kwargs):
# 获取对象,会检查对象的权限
instance = self.get_object()
# 序列化
serializer = self.get_serializer(instance)
return Response(serializer.data)
For example:
class BookDetailView(RetrieveModelMixin, GenericAPIView):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
def get(self, request, pk):
return self.retrieve(request)
4)UpdateModelMixin
Updated view expansion class provides update(request, *args, **kwargs)
methods, can be implemented quickly update an existing data object.
It also provides partial_update(request, *args, **kwargs)
methods may be implemented partial update.
200 successful return, when the data serializer check fails, returns 400 an error.
Source:
class UpdateModelMixin(object):
"""
Update a model instance.
"""
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
return Response(serializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
5)DestroyModelMixin
Delete View extension class is provided destroy(request, *args, **kwargs)
a method, it can be implemented quickly delete an existing data object.
The successful return of 204, 404 there is no return.
Source:
class DestroyModelMixin(object):
"""
Destroy a model instance.
"""
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
def perform_destroy(self, instance):
instance.delete()
Several subclasses view
1)CreateAPIView
Provide post method
Inherited from: GenericAPIView, CreateModelMixin
2)ListAPIView
Provide the get method
Inherited from: GenericAPIView, ListModelMixin
3)RetrieveAPIView
Provide the get method
Inherited from: GenericAPIView, RetrieveModelMixin
4) DestoryAPIView
Provided delete method
Inherited from: GenericAPIView, DestoryModelMixin
5)UpdateAPIView
Offers put and patch method
Inherited from: GenericAPIView, UpdateModelMixin
6)RetrieveUpdateAPIView
Provide get, put, patch method
继承自: GenericAPIView、RetrieveModelMixin、UpdateModelMixin
7)RetrieveUpdateDestoryAPIView
Provide get, put, patch, delete method
继承自:GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestoryModelMixin
View Set ViewSet
Using the set of views ViewSet, a series of logic operations can be associated into a class:
- list () provides a set of data
- retrieve () provides a single data
- create () to create data
- update () to save data
- destory () to delete data
ViewSet viewset not implemented class get (), post () method and the like, but to realize the operation action as list (), create () and the like.
Using the set of views only as_view () method when the only action the operation mode corresponding to the specific request. Such as:
class BookInfoViewSet(viewsets.ViewSet):
def list(self, request):
books = BookInfo.objects.all()
serializer = BookInfoSerializer(books, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
try:
books = BookInfo.objects.get(id=pk)
except BookInfo.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = BookInfoSerializer(books)
return Response(serializer.data)
When setting the route, we can do this
urlpatterns = [
url(r'^books/$', BookInfoViewSet.as_view({'get':'list'}),
url(r'^books/(?P<pk>\d+)/$', BookInfoViewSet.as_view({'get': 'retrieve'})
]
Common View Set parent
1) ViewSet
Inherited from APIView
the ViewSetMixin
role also APIView essentially similar, provides authentication, verification authority, traffic management.
Mapping processing: ViewSet achieved when an incoming call as_view () dictionary (e.g., { 'list' 'get'}) mainly through inheritance ViewSetMixin.
In ViewSet, the action does not provide any method of action, we need to implement their own action methods.
2)GenericViewSet
Use ViewSet usually not easy, because the list, retrieve, create, update, destory other methods need to write your own, but these methods with the same name, said before the extension class Mixin provided, so we can re-extend the class through inheritance Mixin with these methods without having to write your own. But Mixin dependence and extension classes GenericAPIView
, so it needs to inherit GenericAPIView
.
GenericViewSet helped us complete this work inheritance, inherited from GenericAPIView
and ViewSetMixin
, in the realization of the dictionary when incoming calls as_view () (such as {'get':'list'}
) the mapping process to work, while also providing a GenericAPIView
basis for the methods provided, can directly extend the class with Mixin use.
For example:
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
class BookInfoViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
The definition of url
urlpatterns = [
url(r'^books/$', views.BookInfoViewSet.as_view({'get': 'list'})),
url(r'^books/(?P<pk>\d+)/$', views.BookInfoViewSet.as_view({'get': 'retrieve'})),
]
3)ModelViewSet
Inherited from GenericViewSet
, and including ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestoryModelMixin.
4)ReadOnlyModelViewSet
Inherited from GenericViewSet
, and including ListModelMixin, RetrieveModelMixin.
Viewset define additional action action
In view of the concentration, in addition to the default method of operation, may further add custom actions.
For example:
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
class BookInfoViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
def latest(self, request):
"""
返回最新的图书信息
"""
book = BookInfo.objects.latest('id')
serializer = self.get_serializer(book)
return Response(serializer.data)
def read(self, request, pk):
"""
修改图书的阅读量数据
"""
book = self.get_object()
book.bread = request.data.get('read')
book.save()
serializer = self.get_serializer(book)
return Response(serializer.data)
The definition of url
urlpatterns = [
url(r'^books/$', views.BookInfoViewSet.as_view({'get': 'list'})),
url(r'^books/latest/$', views.BookInfoViewSet.as_view({'get': 'latest'})),
url(r'^books/(?P<pk>\d+)/$', views.BookInfoViewSet.as_view({'get': 'retrieve'})),
url(r'^books/(?P<pk>\d+)/read/$', views.BookInfoViewSet.as_view({'put': 'read'})),
]
action properties
In view of the focus, we may be obtained by action action action object properties when the current request is which set of views.
E.g:
from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet
from booktest.models import BookInfo
from .serializers import BookInfoModelSerializer
from rest_framework.response import Response
class BookInfoModelViewSet(ModelViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoModelSerializer
def get_top_5(self,request):
"""获取评论值最多的5条数据"""
# 操作数据库
print(self.action) # 获取本次请求的视图方法名
通过路由访问到当前方法中.可以看到本次的action就是请求的方法名
Routing Routers
For views Viewset set, we can in addition to manually correspondence between the operation and action specified in the request mode, but also can help us to quickly Routers use routing information.
REST framework provides two router
- SimpleRouter
- DefaultRouter
Instructions
1) Create a router object and registered view set, for example,
from rest_framework import routers
router = routers.SimpleRouter()
router.register(r'books', BookInfoViewSet, base_name='book')
register(prefix, viewset, base_name)
- The prefixes prefix viewset
- viewset set of views
- Base_name routing prefix name
The routing code described above is formed as follows:
^books/$ name: book-list
^books/{pk}/$ name: book-detail
2) adding routing data
There are two ways:
urlpatterns = [
...
]
urlpatterns += router.urls
or
urlpatterns = [
...
url(r'^', include(router.urls))
]
Centralized view additional action statement
In view of the focus, if you want to help us let Router automatically generate routing information for a custom action, you need to use rest_framework.decorators.action
a decorator.
In action decorator decoration method name will be the name of the action as the action, the list, retrieve equivalents.
Decorative action may receive two parameters:
Methods : declare the action corresponding to the request method, a list of transfer
detail
: Declare the action whether the path corresponding to a single resource, and whether it is
xxx/<pk>/action方法名/
- True represents the path format is
xxx/<pk>/action方法名/
- It represents the path format is False
xxx/action方法名/
- True represents the path format is
For example:
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
class BookInfoViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
queryset = BookInfo.objects.all()
serializer_class = BookInfoSerializer
# detail为False 表示路径名格式应该为 books/latest/
@action(methods=['get'], detail=False)
def latest(self, request):
"""
返回最新的图书信息
"""
...
# detail为True,表示路径名格式应该为 books/{pk}/read/
@action(methods=['put'], detail=True)
def read(self, request, pk):
"""
修改图书的阅读量数据
"""
...
Automatically by the router node view for this method of forming a customized set of action would be the following:
^books/latest/$ name: book-latest
^books/{pk}/read/$ name: book-read
URL routing router formation of the way
1) SimpleRouter
2)DefaultRouter
DefaultRouter and SimpleRouter difference is, DefaultRouter will be more comes with a default root view of the API returns data in response to a hyperlink that contains a list of all views.