views: Common 1 and 42 types1. The original ApiviewclassIndexview (views. Apiview):defGet (self, request, *args, * *Kwargs): User_list=models. UserInfo.objects.all () Ser= Indexserializer (instance=user_list,many=True)returnResponse (ser.data)2. Genericapiview (Apiview), there are some ways to implement the chicken, no use3. Genericviewset (Viewsetmixin, generics. Genericapiview): Label What the request method does in the URL to perform what function route modification: Urlpatterns=[url (r'^index/$', views. Indexview.as_view ({'Get':'List','Post':'Create'}), url (r'^index/(? p<pk>\d+) $', views. Indexview.as_view ({'Get':'Retrieve','Delete':'Remove'}),] View modification:classIndexview (viewsets. Genericviewset):defList (self,request,*args,**Kwargs):Pass #Get list information defRetrieve (self, request, *args, * *Kwargs):Pass #get A single piece of data defCreate (Self,request, *args, * *Kwargs):Passthe way to not genericviewset before: increase POST/users/Delete Delete/users/1/change all changes to PUT/users/1/Patch Partial modification/users/1/Check GET all data/users/GET a piece of data/users/1/Urlpatterns=[url (r'^index/$', views. Indexview.as_view ()), URL (r'^index/(? p<pk>\d+) $', views. Indexview.as_view ()),]classIndexview (views. Apiview):defGet (self,request,*args,**Kwargs): PK= Kwargs.get ('PK') ifPK:Pass #get A single piece of information Else: Pass #Get list information defPost (self,request,*args,**Kwargs):Pass defPut (self,request,*args,**Kwargs):Pass defPatch (self,request,*args,**Kwargs):Pass defDelete (self,request,*args,**Kwargs):Pass4. Modelviewset (mixins. Createmodelmixin,mixins. Retrievemodelmixin,mixins. Updatemodelmixin,mixins. Destroymodelmixin,mixins. Listmodelmixin,genericviewset)#inherit the additions and deletions and change the local update +genericviewset fromRest_framework.paginationImportlimitoffsetpagination,pagenumberpagination,cursorpagination fromRest_frameworkImportviewsets fromApiImportModels fromApi.serializers.indexImportIndexserializerclassP2 (pagenumberpagination):#number of data bars displayed per pageMax_page_size = 5page_size= 2Page_size_query_param='size' #Page NumberPage_query_param ='page' classIndexview (viewsets. Modelviewset):#you can also not inherit Modelviewset, inherit a few of the above classes, there are only a few methodsQueryset =models. UserInfo.objects.all () Serializer_class=Indexserializer Pagination_class=P2#You can also customize the method #def list (self, request, *args, **kwargs): #Pass #def retrieve (self, request, *args, **kwargs): #Pass
View of the Python-django Rest Framework Framework