The first way of view trilogy: Using a Blend (mixins):
serialize.py
class authormodelserializers (serializers. Modelserializer): class Meta: = Author "__all__ "
urls.py
URL (r'authors/$', views. Authoview.as_view (), name="authors"), url (r'authors/(? p<pk>\d+)/$', views. Authordetailview.as_view (), name="authordetail")
views.py
fromRest_frameworkImportmixins fromRest_frameworkImportgenericsclassAuthoview (mixins. Listmodelmixin,mixins. Createmodelmixin,generics. Genericapiview): Queryset=Author.objects.all () Serializer_class=authormodelserializersdefGet (self,request,*args,**Kwargs):returnSelf.list (request,*args,**Kwargs)defPost (self,request,*args,**Kwargs):returnSelf.create (request,*args,**Kwargs)classAuthordetailview (mixins. Retrievemodelmixin,mixins. Destroymodelmixin,mixins. Updatemodelmixin,generics. Genericapiview): Queryset=Author.objects.all () Serializer_class=authormodelserializersdefGet (self,request,pk,*args,**Kwargs):returnSelf.retrieve (request,pk,*args,**Kwargs)defDelete (self,request,pk,*args,**Kwargs):returnSelf.destroy (request,pk,*args,**Kwargs)defPut (self,request,pk,*args,**Kwargs):returnSelf.update (Request,pk,*args,**kwargs)
Because each table has a repeat operation that makes the code redundant, using the encapsulated mixin is a little more concise,
Mixin: listmodelmxin #查看 createmodelmixin #新建 destorymodelmixin #删除 Retrievemodelmixin #查看具体一条 updatemodelmixin #更新
Second way: Use a generic class-based view
By using the Mixin class, we rewrite these views with less code, but we can go further. The rest framework provides a common set of well-mixed (mixed-in) views that we can use to simplify our views.py
modules.
# other unchanged: from Import mixins from Import generics class Authoview (generics. Listcreateapiview): queryset=Author.objects.all () serializer_class= Authormodelserializersclass Authordetailview (generics. Retrieveupdatedestroyapiview): queryset=Author.objects.all () serializer_class= Authormodelserializers
The inheritance relationship has been further encapsulated, the method has been more concise optimization, methods are all written into the source code.
The third way: Viewsets. Modelviewset
views.py
from Import viewsets class Authormodelview (viewsets. Modelviewset): queryset=Author.objects.all () serializer_class=authormodelserializers
urls.py
URL (r'authors/$', views. Authormodelview.as_view ({"Get":"List","Post":"Create"}), Name="authors"), url (r'authors/(? p<pk>\d+)/$', views. Authormodelview.as_view ({"Get":"Retrieve","put":"Update","Delete":"Destroy"}), Name="Authordetail"),
2 sailings using the same class, add the parameters to the As_view () in the URLs, so that different requests take different methods.
Here to see the source of a weak point encountered, here to record:
defFoo (action=none,**Kwargs):Print(Action)Print(Kwargs) foo (**{"D": 5})Print("______________") foo ({"D": 5})#Summary: * * Dictionary is equivalent to keyword transfernone{'D': 5}______________{'D': 5}{}
This method of communication needs to be noted.
Summarize:
The part of the code that was picked up from the source is the final process.
process: (1) URL (r'^authors/$', views. Authormodelview.as_view ({"Get":"List","Post":"Create"}), Name="author"), (2) URL (r'^authors/$', Viewsetmixin.as_view ({"Get":"List","Post":"Create"}), Name="author"), (3) URL (r'^authors/$', Viewsetmixin Class View), once accessed/authors/: Viewsetmixindefview (): formethod, actioninchActions.items ():#{"Get": "List", "POST": "Create"}Handler = GetAttr (self, action)#self.list self.createSetAttr (self, method, handler) #self. Get = Self.list Self.post=self.post #这里取到了get, Post and other methods, assigned to handler, and then through SetAttr ()
#执行dispatch方法 Self.dispatch (Request,*args, * *Kwargs)
The Self.dispatch #这个dispatch方法是APIView里面的 under the Apiview class, returning the response. #Distribution ifRequest.method.lower ()inchSelf.http_method_names:
#getattr (self, "get")--->self.list
#getattr (self, "POST")--->self.create
Handler=GetAttr (Self,request.method.lower ()) #这里真正执行的是list, create method.
#最终执行handler (). Response= Handler (request, *args, **kwargs)#self.list ()
#将response返回.returnResponse
Evolutionary trilogy of views