Serialization of DRF

Source: Internet
Author: User

DRF View

Apiview

When writing CBV in Django, It inherits view, while rest_framework inherits apiview. What are the differences between them?

urlpatterns = [    url(r‘^book$‘, BookView.as_view()),    url(r‘^book/(?P<id>\d+)$‘, BookEditView.as_view()),]

Whether it is view or apiview, The as_view () method is called at the beginning. What is the difference between the source code?

We can see that apiview inherits the view and executes the as_view () method in the view. Finally, the view is returned, and csrf authentication is removed after being wrapped in the csrf_exempt () method.

Then, let's see what the as_view () method does in the view.

The as_view method in view returns the view function, while the view function executes self. dispatch () method, but here the dispatch method should be searched from itself in order, and the dispatch method is also written in apiview. Therefore, the dispatch method in apiview should be executed.

Here, I need to pay attention to what initialize_request has done.

Here, an instantiated object is returned.

This will change Django's original request to self. _ Request

 

 

 

 

View

Self-written version

# Self-rewriting view from rest_framework.views import apiviewfrom response import responsefrom response import viewsetmixin # Get the data class genericapiview (apiview): queryset = none serializer_class = none # Get queryset def get_queryset (Self): return self. queryset. all () # Get the serializer def get_serializer (self, * ARGs, ** kwargs): return self. serializer_class (* ARGs, ** kwargs) # Get method (multiple queries) Class listmodelmixin (object): def list (self, request): queryset = self. get_queryset () ser_obj = self. get_serializer (queryset, counter = true) return response (ser_obj.data) # query a single class retrievemodelmixin (object): def retrieve (self, request, ID): stu_obj = self. get_queryset (). filter (ID = ID ). first () ser_obj = self. get_serializer (stu_obj) return response (ser_obj.data) # POST method class createmodelmixin (object): def create (self, request): ser_obj = self. get_serializer (Data = request. data) If ser_obj.is_valid (): ser_obj.save () return response (ser_obj.data) else: return response (ser_obj.errors) # Put/patch method: modify a single class updatemodelmixin (object ): def Update (self, request, ID): stu_obj = self. get_queryset (). filter (ID = ID ). first () ser_obj = self. get_serializer (instance = stu_obj, Data = request. data, partial = true) If ser_obj.is_valid (): ser_obj.save () return response (response) return response (ser_obj.errors) # Delete class destroymodelsmixin (object): def destroy (self, request, request, ID): stu_obj = self. get_queryset (). filter (ID = ID ). first () If stu_obj: stu_obj.delete () return response ("") return response ("the deleted object does not exist") # class modelviewset (viewsetmixin, genericapiview, listmodelmixin, retrievemodelmixin, createmodelmixin, updatemodelmixin, destroymodelsmixin,): passclass student (modelviewset): queryset = models. student. objects serializer_class = studentserializer
Self-Rewriting

 

He also has a built-in module, which is the same as this one.

from rest_framework.viewsets import ModelViewSetclass Student(ModelViewSet):    queryset = models.Student.objects    serializer_class = StudentSerializer
Built-in modules

 

URLs. py code

 

 

 

 

DRF route

This route is actually encapsulated, but the better the encapsulation, the more restrictions on application scenarios.

from .views import BookViewfrom rest_framework.routers import DefaultRouterrouter = DefaultRouter()router.register(r"book", BookView)urlpatterns = [    # url(r‘^book$‘, BookView.as_view()),    # url(r‘^book/(?P<id>\d+)$‘, BookEditView.as_view()),    # url(r‘^book$‘, BookView.as_view({"get": "list", "post": "create"})),    # url(r‘^book/(?P<pk>\d+)$‘, BookView.as_view({"get": "retrieve", "patch": "update", "delete": "destroy"})),]urlpatterns += router.urls
Routing

 

Serialization of DRF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.