View of the Python-django Rest Framework Framework

Source: Internet
Author: User

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

Related Article

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.