Django REST framework视图

混合类阶段(封装2次)

路由:

url(r'school/$', views.SchoolView.as_view()),
url(r'school/(?P<pk>\d+)/$', views.SchoolDetail.as_view()),

 

序列化:


from rest_framework import serializers
from app01 import models
class AuthorModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.Author
        fields = "__all__"

 

视图:

查询所有:

from rest_framework import mixins
from rest_framework import generics


class AuthorView(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

 

查询单条:

class AuthorDetaiView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin, mixins.UpdateModelMixin,
                      generics.GenericAPIView):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def delete(self,request,*args,**kwargs):
        return self.destroy(request,*args,**kwargs)

    def put(self,request,*args,**kwargs):
        return self.update(request,*args,**kwargs)

 

使用通用的基于类的视图(封装第3次)

路由:

url(r'school/$', views.SchoolView.as_view()),
url(r'school/(?P<pk>\d+)/$', views.SchoolDetail.as_view()),

 

序列化:

from rest_framework import serializers
from app01 import models
class AuthorModelSerializers(serializers.ModelSerializer): class Meta: model = models.Author fields = "__all__"

 

视图:

from rest_framework import mixins
from rest_framework import generics


# 封装了3层
class AuthorView(generics.ListCreateAPIView):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers

 

 

使用视图集

路由:

  re_path('^authors/$', views.AuthorView.as_view({"get": "list", "post": "create"})),
  re_path('^author/(?P<pk>\d+)/$',views.AuthorDetaiView.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),
    

 

序列化:

from rest_framework import serializers
from app01 import models
class AuthorModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.Author
        fields = "__all__"

 

视图:

# 封装了3层
class AuthorView(viewsets.ModelViewSet):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers

 

高级路由

其他同上:

路由

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'school', views.SchoolView)
urlpatterns += router.urls

默认路由

 

from django.contrib import admin
from django.urls import path,re_path
from app01 import views
from rest_framework import  routers
from django.conf.urls import url,include


routers = routers.DefaultRouter()
routers.register("authors", views.AuthorModelView)


urlpatterns = [
    re_path(r'^',include(routers.urls)),

]
posted @ 2019-12-18 01:33  Mr_Riven  阅读(254)  评论(0编辑  收藏  举报